本文实例为大家分享了PHP AjaxForm提交图片上传并显示图片的具体代码,供大家参考,具体内容如下
$data['status'] = 1;
$data['msg'] = "文件上传失败,文件大小".$file_size."超过限制,允许上传大小".sizeFormat($file_allow_size)."!";
break;
case 3:
$data['status'] = 1;
$data['msg'] = "上传失败,文件只有部份上传!";
break;
case 4:
$data['status'] = 1;
$data['msg'] = "上传失败,文件没有被上传!";
break;
case 5:
$data['status'] = 1;
$data['msg'] = "文件上传失败,文件大小为0!";
break;
}
if(stripos($file_allow_ext,$file_ext)===false){
$data['status'] = 1;
$data['msg'] = "该文件扩展名不允许上传";
}
if($file_size>$file_allow_size){
$data['status'] = 1;
$data['msg'] = "文件大小超过限制,只能上传".sizeFormat($file_allow_size)."的文件!";
}
if($data['status']==1){
$data['status'] = 1;
$data['msg'] = $data['msg'];
exit(json_encode($data));
}
if($data['status']==0){
if(file_exists($file_upload)){
$file_newname = date("YmdHis").''.rand(10000,99999).'.'.$file_ext;
$file_save_path = $file_upload.$file_new_name;
$data['status'] = 0;
$data['url'] = $file_save_path;
move_uploaded_file($file_tmp_name,$file_save_path);
exit(json_encode($data));
}else{
exit(json_encode($data));
}
}
}
}
function sizeFormat($size)
{
$sizeStr='';
if($size<1024)
{
return $size."bytes";
}
else if($size<(10241024))
{
$size=round($size/1024,1);
return $size."KB";
}
else if($size<(102410241024))
{
$size=round($size/(10241024),1);
return $size."MB";
}
else
{
$size=round($size/(102410241024),1);
return $size."GB";
}
}
?>
HTML如下