当不添加图片的时候总是编辑失败,请老师帮忙看看代码
upload.fnc.php
<?php
/**
* 构建上传文件信息
* @return array
*/
function buildInfo(){
if(!$_FILES){
return ;
}
$i=0;
foreach($_FILES as $v){
//单文件
if(is_string($v['name'])){
$files[$i]=$v;
$i++;
}else{
//多文件
foreach($v['name'] as $key=>$val){
$files[$i]['name']=$val;
$files[$i]['size']=$v['size'][$key];
$files[$i]['tmp_name']=$v['tmp_name'][$key];
$files[$i]['error']=$v['error'][$key];
$files[$i]['type']=$v['type'][$key];
$i++;
}
}
}
return $files;
}
function uploadFile($path="uploads",$allowExt=array("gif","jpeg","png","jpg","wbmp"),$maxSize=2097152,$imgFlag=true){
if(!file_exists($path)){
mkdir($path,0777,true);
}
$i=0;
$files=buildInfo();
if(!($files&&is_array($files))){
return ;
}
foreach($files as $file){
if($file['error']===UPLOAD_ERR_OK){
$ext=getExt($file['name']);
//检测文件的扩展名
if(!in_array($ext,$allowExt)){
exit("非法文件类型");
}
//校验是否是一个真正的图片类型
if($imgFlag){
if(!getimagesize($file['tmp_name'])){
exit("不是真正的图片类型");
}
}
//上传文件的大小
if($file['size']>$maxSize){
exit("上传文件过大");
}
if(!is_uploaded_file($file['tmp_name'])){
exit("不是通过HTTP POST方式上传上来的");
}
$filename=getUniName().".".$ext;
$destination=$path."/".$filename;
if(move_uploaded_file($file['tmp_name'], $destination)){
$file['name']=$filename;
unset($file['tmp_name'],$file['size'],$file['type']);
$uploadedFiles[$i]=$file;
$i++;
}
}else{
switch($file['error']){
case 1:
$mes="超过了配置文件上传文件的大小";//UPLOAD_ERR_INI_SIZE
break;
case 2:
$mes="超过了表单设置上传文件的大小"; //UPLOAD_ERR_FORM_SIZE
break;
case 3:
$mes="文件部分被上传";//UPLOAD_ERR_PARTIAL
break;
case 4:
$mes="没有文件被上传1111";//UPLOAD_ERR_NO_FILE
break;
case 6:
$mes="没有找到临时目录";//UPLOAD_ERR_NO_TMP_DIR
break;
case 7:
$mes="文件不可写";//UPLOAD_ERR_CANT_WRITE;
break;
case 8:
$mes="由于PHP的扩展程序中断了文件上传";//UPLOAD_ERR_EXTENSION
break;
}
echo $mes;
}
}
return $uploadedFiles;
}
pro.inc.php
function editPro($id){
$arr=$_POST;
$path="./uploads";
$uploadFiles=uploadFile($path);
if(is_array($uploadFiles)&&$uploadFiles){
foreach($uploadFiles as $key=>$uploadFile){
thumb($path."/".$uploadFile['name'],"../image_50/".$uploadFile['name'],50,50);
thumb($path."/".$uploadFile['name'],"../image_220/".$uploadFile['name'],220,220);
thumb($path."/".$uploadFile['name'],"../image_350/".$uploadFile['name'],350,350);
thumb($path."/".$uploadFile['name'],"../image_800/".$uploadFile['name'],800,800);
}
}
$where="id={$id}";
$res=update("imooc_pro",$arr,$where);
$pid=$id;
if($res&&$pid){
if($uploadFiles &&is_array($uploadFiles)){
foreach($uploadFiles as $uploadFile){
$arr1['pid']=$pid;
$arr1['albumPath']=$uploadFile['name'];
addAlbum($arr1);
}
}
$mes="<p>编辑成功!</p><a href='listPro.php' target='mainFrame'>查看商品列表</a>";
}else{
if(is_array($uploadFiles)&&$uploadFiles){
foreach($uploadFiles as $uploadFile){
if(file_exists("../image_800/".$uploadFile['name'])){
unlink("../image_800/".$uploadFile['name']);
}
if(file_exists("../image_50/".$uploadFile['name'])){
unlink("../image_50/".$uploadFile['name']);
}
if(file_exists("../image_220/".$uploadFile['name'])){
unlink("../image_220/".$uploadFile['name']);
}
if(file_exists("../image_350/".$uploadFile['name'])){
unlink("../image_350/".$uploadFile['name']);
}
}
}
$mes="<p>编辑失败!</p><a href='listPro.php' target='mainFrame'>重新编辑</a>";
}
return $mes;
}