文件上传成功了,但是返回了一个null是什么原因
来源:2-15 文件上传函数封装
慕侠6347478
2019-11-01 11:33:52
file.func.php
function upload_file(array $fileInfo,string $uploadPath='./upload',bool $imageFlag=true,array $allowExt=
['jpg','jpeg','png','gif'],int $maxSize=2079152){
define('UPLOAD_ERRS',[
'upload_max_filesize'=>'超过PHP配置文件中的upload_max_filesize选项的值',
'form_max_size'=>'超过了表单MAX_FILE_SIZE选项的值',
'upload_file_partial'=>'部分文件被上传',
'no_upload_file_select'=>'没有选择上传文件',
'upload_system_error'=>'系统错误',
'no_allow_ext'=>'非法文件类型',
'exceed_max_size'=>'超出允许上传的最大值',
'not_true_image'=>'文件不是真实图片',
'not_http_post'=>'文件不是通过http_post方式上传的',
'move_error'=>'文件移动失败'
]);
//检测是否上传有错误
if($fileInfo['error']===UPLOAD_ERR_OK){
//检测上传文件类型
$ext=strtolower(pathinfo($fileInfo['name'],PATHINFO_EXTENSION));
if(!in_array($ext,$allowExt)){
return UPLOAD_ERRS['no_allow_ext'];
}
//检测上传文件大小是否符合规范
if($fileInfo['size']>$maxSize){
return UPLOAD_ERRS[''];
}
//检测是否是真实图片
if($imageFlag){
if(@!getimagesize($fileInfo['tmp_name'])){
return UPLOAD_ERRS['not_true_image'];
}
}
//检测文件是否通过HTP POST方式上传上来的
if(!is_uploaded_file($fileInfo['tmp_name'])){
return UPLOAD_ERRS['not_http_post'];
}
//判断检测目录目标是否存在,不存在则创建
if(!is_dir($uploadPath)){
mkdir($uploadPath,0777,true);
}
//生成唯一文件名,防止重名产生覆改覆盖
$uniName=md5(uniqid(microtime(true),true)).'.'.$ext;
$dest=$uploadPath.DIRECTORY_SEPARATOR.$uniName;
$dest=$uploadPath.DIRECTORY_SEPARATOR.$uniName;
//通过以上全部检测后,移动文件
if(@!move_uploaded_file($fileInfo['tmp_name'],$dest)){
return UPLOAD_ERRS['move_error'];
}
}else{
switch($fileInfo['errot']){
case 1:
$mes=UPLOAD_ERRS['upload_max_filesize'];
break;
case 2:
$mes=UPLOAD_ERRS['form_max_size'];
break;
case 3:
$mes=UPLOAD_ERRS['upload_file_partial'];
break;
case 4:
$mes=UPLOAD_ERRS['no_upload_file_select'];
break;
case 6:
case 7:
case 8:
$mes=UPLOAD_ERRS['upload_system_error'];
break;
}
echo $mes;
return false;
}
}
doUpload.php
<?php
require_once 'file.func.php';
$fileInfo=$_FILES['myFile'];
var_dump(upload_file($fileInfo));
upload.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>上传</title>
</head>
<body>
<form action="doUpload.php" method="post" enctype="multipart/form-data">
<input type="file" name="myFile" id="">
<input type="submit" value="立即上传">
</form>
</body>
</html>
1回答
同学你好,因同学上传成功后未返回数据,所以返回是null。同学可以直接输出true。修改后代码如下:
祝学习愉快!
相似问题