php后台接受上传图片

配合上个博文的,捣鼓了一个php后台的东西,虽然不怎么懂,但慢慢来,还是可以的。传送门

java太不好学了,玩玩php,满足一下自己。

那个图片压缩的效果很明显,居然压缩了好几倍。

php
header('Content-type:text/html; charset="utf-8"');
$base64_image_content=$_POST["file"];
//var_dump($$base64_image_content);
if(preg_match('/^(data:\s*image\/(\w+);base64,)/i',$base64_image_content,$result)){
    $type=$result[2];
    $new_file="upload/".date("Ymd",time())."/";
    if(!file_exists($new_file)){
        /*权限问题,0777是最好的,其他可能会被禁止*/
        mkdir($new_file,0777);
        echo "创建成功";
    }else{
        echo "创建失败";
    }
    $new_file=$new_file.time().".{
     $type}";
    if(file_put_contents($new_file,base64_decode(str_replace($result[1],"",$base64_image_content)))){
        echo "成功";
    }else{
        echo "失败";
    }
    
}else{
    echo "不匹配!!";
}
?>

表单提交,传送图片

php
header('Content-type:text/html; charset="utf-8"');
$upload_dir = 'uploads/';

if(strtolower($_SERVER['REQUEST_METHOD']) != 'post'){
    exit_status(array('code'=>1,'msg'=>'错误提交方式'));
}

if(array_key_exists('file',$_FILES) && $_FILES['file']['error'] == 0 ){
    
    $pic = $_FILES['file'];
    
    if(move_uploaded_file($pic['tmp_name'], $upload_dir.$pic['name'])){
        exit_status(array('code'=>0,'msg'=>'上传成功','url'=>$upload_dir.$pic['name']));
    }
    
}
echo $_FILES['file']['error'];
exit_status(array('code'=>1,'msg'=>'出现了一些错误'));

function exit_status($str){
    echo json_encode($str);
    exit;
}
?>

 

转载于:https://www.cnblogs.com/zhangzhicheng/p/6642468.html

你可能感兴趣的:(php,java)