已经var_dump($allowExt)确实是个数组,为什么还出现上面错误?
upload.html:
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title></head><body><!-- The data encoding type, enctype, MUST be specified as below --><form enctype="multipart/form-data" action="doAction3.php" method="POST"> <!-- MAX_FILE_SIZE must precede the file input field --> <input type="hidden" name="MAX_FILE_SIZE" value="3000000" /> <!-- Name of input element determines name in $_FILES array --> 选择文件: <input name="userfile" type="file" /> <input type="submit" value="上传" /></form></body></html>
upload.function.php:
<?php/* * $filename = $_FILES['userfile']['name']; * $filetype = $_FILES['userfile']['type']; * $filetmp_name = $_FILES['userfile']['tmp_name']; * $filesize = $_FILES['userfile']['size']; * $fileerror = $_FILES['userfile']['error']; */ $fileInfo = $_FILES['userfile'];/*** * * @param unknown $fileInfo必选参数 * @param array $allowExt * @param unknown $maxsize * @param string $path * @param string $flag是否开启图片真实性检测 */function uploadFile($fileInfo, $allowExt = ['jpg','jpeg','gif','png','zip'], $maxsize = 1 * 1024 * 1024, $path = "uploads", $flag = true){ if ($fileInfo['error'] > 0) { switch ($fileInfo['error']) { case 1: $mes = "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。 "; break; case 2: $mes = "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。 "; break; case 3: $mes = "文件只有部分被上传 "; break; case 4: $mes = "没有文件被上传 "; break; case 6: $mes = "找不到临时文件夹。 "; break; case 7: case 8: $mes = "系统错误"; break; } exit($mes); } // 检测上传文件的类型 $ext = pathinfo($fileInfo['name'])['extension']; /* * $allowExt = [ * 'jpeg', * 'jpg', * 'gif', * 'png', * 'wbmp', * 'rar' * ]; */ if (!in_array($ext, $allowExt)) { exit("非法文件类型"); } // 检测是不是真实的图片文件 // $flag = true; if ($flag) { if (@! getimagesize($fileInfo['tmp_name'])) { exit("上传的不是真实的图片文件"); } } // 检测文件大小 // $maxsize = 1 * 1024 * 1024; if ($fileInfo['size'] > $maxsize) { exit("上传文件太大!"); } // 检测文件是不是是通过HTTP_POST方式上传的 if (! is_uploaded_file($fileInfo['tmp_name'])) { exit("文件不是通过httppost方式上传的"); } // 移动文件 // $path = "uploads"; // 如果目录不存在创建目录 if (! file_exists($path)) { mkdir($path, 0777, true); chmod($path, 0777); } // 防止文件重名覆盖 $uniName = md5(uniqid(microtime(true), true)) . '.' . $ext; $destination = $path . '/' . $uniName; if (! move_uploaded_file($fileInfo['tmp_name'], $destination)) { echo "文件上传失败"; } else { return $destination; }}
doAction3.php:
<?phpinclude_once 'upload.function.php';$fileInfo = $_FILES['userfile'];$allowExt = ['jpg','jpeg','gif','png','zip'];$maxsize = 1 * 1024 * 1024;$path = "uploads";$flag = false;// var_dump($allowExt,$flag,$maxsize);die();$newName = uploadFile($fileInfo,$flag,$allowExt);// var_dump($newName);
找到原因了,函数调用的时候参数顺序搞错了,导致的问题