2-16 文件上传函数封装
来源:2-15 文件上传函数封装
jujijigo
2018-06-03 11:57:18
以下是我跟着视频在file.func.php文件打的函数定义代码:
function upload_file(array $fileInfo, string $uploadPath='./uploads,', bool $imageFlag=true, array $allowExt=array('jpeg', 'jpg', 'png', 'gif'), int $maxSize=2097152) { 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['exceed_max_size']; } // 检测是否是真实图片 if ($imageFlag) { if (@!getimagesize($fileInfo['tmp_name'])) { return UPLOAD_ERRS['not_true_image']; } } // 检测是否通过HTTP 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; // 移动文件 if (@!move_uploaded_file($fileInfo['tmp_name'], $dest)) { return UPLOAD_ERRS['move_error']; } return $dest; } else { switch($fileInfo['error']) { case 1: // $mes = '超过了PHP配置文件中upload_max_filesize选项的值'; $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; } return $mes; } }
以下是upload.html代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <h1>文件上传</h1> <form action="doUpload.php" method="POST" enctype="multipart/form-data"> <input type="file" name="myFile" id=""> <input type="submit" value="立即上传"> </form> </body> </html>
以下是doUpload.php代码:
<?php require_once 'file.func.php'; $fileInfo = $_FILES['myFile']; upload_file($fileInfo); var_dump(upload_file($fileInfo));
以下是在浏览器执行upload.html然后上传文件执行的错误信息:
检查not_true_image相关代码块找不到问题,切换php版本也一样错误,代码其他地方找了好久也找不出问题出在哪里,请老师指点下问题出在哪里了,谢谢!
1回答
你好,相同函数传递参数应相同,建议参考一下代码;
<?php function upload_file($fileInfo){ $uploadPath='./uploads'; $imageFlag=true; $allowExt=array('jpeg', 'jpg', 'png', 'gif'); $maxSize=2097152; 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['exceed_max_size']; } // 检测是否是真实图片 if ($imageFlag) { if (@!getimagesize($fileInfo['tmp_name'])) { return UPLOAD_ERRS ['not_true_image']; } } // 检测是否通过HTTP 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; // 移动文件 if (@!move_uploaded_file($fileInfo['tmp_name'], $dest)) { return UPLOAD_ERRS['move_error']; } return $dest; } else { switch ($fileInfo['error']) { case 1: // $mes = '超过了PHP配置文件中upload_max_filesize选项的值'; $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; } return $mes; } }
上传结果:
如果解决了您的问题请采纳,祝学习愉快!
相似问题