|
本帖最后由 seafood_to 于 2018-6-14 10:38 编辑
学习php但是网站的一个图片上传功能用不了
页面显示上传成功函数也不报错但是目标文件就是不存在
求助php大佬!!!
表单提交部分代码:
" method="post" enctype="multipart/form-data">
......
......
接收上传部分代码:
$image = $_FILES['pic']['tmp_name'];
......
if(!empty($image)){
$path = PATH.'upload/goods/';
注:define('PATH', str_replace('\\','/',dirname(__FILE__)).'/../');
$filename = upload('pic',$path);
$path = PATH.'upload/goods/'.$filename;
$newname = zoom($path,200,200);
unlink($path);
}
函数代码:
function upload($field,$path){
if($_FILES[$field]['error'] != 0) exit('上传出错');
list($maintype,$subtype) = explode('/',$_FILES[$field]['type']);
if($maintype != 'image') exit('请上传图片');
if($subtype == 'jpeg') $subtype = 'jpg';
$newfile = md5(uniqid()).'.'.$subtype;
$newpath = rtrim($path,'/').'/'.$newfile;
$res = move_uploaded_file($_FILES[$field]['tmp_name'],$newpath);
if($res){
return $newfile;
}else{
return '上传失败';
}
}
function zoom($path,$width=200,$height=200){
$info = getimagesize($path);
if(!$info) exit('请处理图片');
$arr = explode('/',$info['mime']);
$ext = $arr[1];
$create = 'imagecreatefrom'.$ext;
$save = 'image'.$ext;
list($t_w,$t_h) = $info;
$des_w = $width;
$des_h = $des_w * ($t_h / $t_w);
if($des_h > $height){
$des_h = $height;
$des_w = $height * ($t_w / $t_h);
}else{
$des_w = $width;
$des_h = $des_w * ($t_h / $t_w);
}
$huabu = imagecreatetruecolor($width,$height);
$white = imagecolorallocate($huabu, 255, 255, 255);
imagefill($huabu, 0, 0, $white);
$meizi = $create($path);
$hua_x = ($width - $des_w) / 2;
$hua_y = ($height - $des_h) / 2;
imagecopyresampled($huabu,$meizi, $hua_x,$hua_y, 0,0, $des_w,$des_h, $t_w,$t_h);
if($ext == 'jpeg') $ext = 'jpg';
$filename = basename($path);
$filename = substr($filename,0,strrpos($filename,'.'));
$newname =$filename.'.'.$ext;
$savepath = rtrim(dirname($path),'/').'/'.$newname;
$save($huabu,$savepath);
imagedestroy($huabu);
imagedestroy($meizi);
return $newname;
}
上传的文件夹goods是存在的,函数也返回了新的文件名newfile但是上传的文件就是找不到,用的lnmp.org一键包的lamp文件夹权限WWW:755 |
|