function uploadFile($path = "uploads", $allowExt = array("gif", "jpeg", "png", "jpg", "wbmp"), $maxSize = 2097152, $imgFlag = true) { if (!file_exists($path)) { mkdir($path, 0777, true); } $files = buildInfo(); $i = 0; 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("上传文件过大"); } // 是否通过HTTP POST上传 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['error'], $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 = "没有文件被上传"; // 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 } echo $mes; } } return $uploadedFiles; }
function uploadFile($path, $allowExt = array("gif", "jpeg", "png", "jpg", "wbmp"), $maxSize = 2097152, $imgFlag = true) { $mes = ""; if (!file_exists($path)) { mkdir($path, 0755, 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("Not a file!"); } //校验是否是一个真正的图片类型 if ($imgFlag) { if (!getimagesize($file['tmp_name'])) { exit("Not a real image type!"); } } //上传文件的大小 if ($file['size'] > $maxSize) { exit("Err_MaxSize!"); } if (!is_uploaded_file($file['tmp_name'])) { exit("Not post by HTTP!"); } $filename = getUniName() . "." . $ext; $destination = $path . "/" . $filename; if (move_uploaded_file($file['tmp_name'], $destination)) { $file['name'] = $filename; //discharge temporary file unset($file['tmp_name'], $file['size'], $file['type']); $uploadedFiles[$i] = $file; $i++; } } else { switch ($file['error']) { case 1: $mes = "Error: upload_ERR_INI_SIZE"; //UPLOAD_ERR_INI_SIZE break; case 2: $mes = "UPLOAD_ERR_FORM_SIZE"; //UPLOAD_ERR_FORM_SIZE break; case 3: $mes = "UPLOAD_ERR_PARTIAL"; //UPLOAD_ERR_PARTIAL break; /* case 4: $mes="UPLOAD_ERR_NO_FILE";//UPLOAD_ERR_NO_FILE break; */ /* case 4: $mes="UPLOAD_ERR_NO_FILE";//UPLOAD_ERR_NO_FILE break; */ case 6: $mes = "UPLOAD_ERR_NO_TMP_DIR"; //UPLOAD_ERR_NO_TMP_DIR break; case 7: $mes = "UPLOAD_ERR_CANT_WRITE"; //UPLOAD_ERR_CANT_WRITE; break; case 8: $mes = "UPLOAD_ERR_EXTENSION"; //UPLOAD_ERR_EXTENSION break; } echo $mes; } } return $uploadedFiles; }
/** * 多文件上传 * */ function uploadFiles($allowExt = array("gif", "jpeg", "jpg", "png", "wbmp"), $maxSize = 2097152, $imgFlag = true, $path = "uploads") { // 检查文件夹 if (!file_exists($path)) { mkdir($path, 0777, true); } $i = 0; $files = @buildInfo(); if (!isset($files)) { exit("请不要上传不能被识别的文件,错误:\$" . "_" . "FILES" . " is empty"); } foreach ($files as $file) { $tmp_name = $file['tmp_name']; $error = $file['error']; $size = $file['size']; $type = $file['type']; $name = $file['name']; if ($error == UPLOAD_ERR_OK) { $ext = getExt($name); // 检查文件拓展名 if (!in_array($ext, $allowExt)) { exit("非法文件类型"); } // 检查大小 if ($size > $maxSize) { exit("文件过大"); } // 检查是否是使用POST HTTP方式上传 if (!is_uploaded_file($tmp_name)) { exit("不是使用POST HTTP方式上传"); } // 检查是否是图片类型 if ($imgFlag && !getimagesize($tmp_name)) { exit("不是真正的图片类型"); } $uniName = getUniName() . '.' . $ext; $destination = $path . "/" . $uniName; if (move_uploaded_file($tmp_name, $destination)) { $file['name'] = $uniName; unset($file['tmp_name'], $file['error'], $file['size'], $file['type']); $uploadedFiles[$i] = $file; $i++; } } else { switch ($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 = "没有文件被上传"; //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; }
function uploadFile($path = "uploads", $allowExt = array("gif", "jpeg", "jpg", "png", "wbmp"), $maxSize = 10485760, $imgFlag = true) { //判断并构建目录 if (!file_exists($path)) { mkdir($path, 0777, true); } $i = 0; //获取文件信息 $files = buildInfo(); //循环文件信息 foreach ($files as $file) { //判断是否上传成功 if ($file['error'] == UPLOAD_ERR_OK) { $ext = getExt($file['name']); //检查文件的扩展名 if (!in_array($ext, $allowExt)) { exit("非法文件类型"); } //校验是否是真正的图片类型 if ($imaFlag) { if (getimagesize($file['tmp_name'])) { exit("不是真正的图片类型"); } } //上传文件的大小 if ($file['size'] > $maxSize) { exit("上传文件过大"); } //判断是否通过HTTP POST方式上传上来的 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()函数丢弃数组内相应的数据 unset($file['error'], $file['tmp_name'], $file['size'], $file['type']); //生成数组名赋值 $uploadedFiles[$i] = $file; $i++; } } else { switch ($file['error']) { case 1: $mes = "超过了配置文件上传文件的大小"; //upload_max_filesize = 64M break; case 2: $mes = "超过了表单设置上传文件的大小"; //post_max_size = 10M break; case 3: $mes = "文件部分被上传"; break; case 4: $mes = "没有文件被上传"; break; case 6: $mes = "没有找到临时目录"; break; case 7: $mes = "文件不可写"; break; case 8: $mes = "由于PHP的扩展程序中断了文件上传"; break; } echo $mes; } } return $uploadedFiles; }