每天进步一点点88
2014-12-28 20:18
<?php
/**
* 获取文件信息
* @return array
*/
function getFileInfo() {
$i = 0;
foreach ( $_FILES as $file ) {
if (is_string ( $file ['name'] )) {
$files [$i] = $file;
$i ++;
} else if (is_array ( $file ['name'] )) {
foreach ( $file ['name'] as $key => $value ) {
$files [$i] ['name'] = $file ['name'] [$key];
$files [$i] ['type'] = $file ['type'] [$key];
$files [$i] ['tmp_name'] = $file ['tmp_name'] [$key];
$files [$i] ['error'] = $file ['error'] [$key];
$files [$i] ['size'] = $file ['size'] [$key];
$i ++;
}
}
}
return $files;
}
/**
* 上传文件函数
*
* @param array $fileInfo
* @param array $allowType
* @param int $maxSize
* @param string $flag
* @param string $savePath
* @return array
*/
function uploadFile($fileInfo, $flag = true, $savePath = "uploads") {
$allowType = array (
'jpeg',
'jpg',
'png',
'gif'
);
$maxSize = 1000000;
$fileType = pathinfo ( $fileInfo ['name'], PATHINFO_EXTENSION );
echo $fileInfo ['error'];
if ($fileInfo ['error'] === UPLOAD_ERR_OK) {
if ($fileInfo ['size'] > $maxSize) {
$info ['mes'] = $fileInfo ['name'] . "上传文件过大";
}
// 判断文件类型
$extension = pathinfo ( $fileInfo ['name'], PATHINFO_EXTENSION );
if (! in_array ( $extension, $allowType )) {
$info ['mes'] =$fileInfo ['name'] ."文件类型不合法,本站只允许上传jpg png gif jpeg" ;
}
if (! is_uploaded_file ( $fileInfo ['tmp_name'] )) {
$info ['mes'] = $fileInfo ['name'] . "上传文件不是通过HTTP POST方式上传的";
}
if ($flag) {
if (! getimagesize ( $fileInfo ['tmp_name'] )) {
$info ['mes'] = $fileInfo ['name'] . "上传文件不是真实的图片类型";
}
}
// $savePath = "uploads";
if (! file_exists ( $savePath )) {
mkdir ( $savePath, 0777, true );
chmod ( $savePath, 0777 );
}
$newName = md5 ( uniqid ( microtime () ) ) . '.' . $fileType;
$desPath = $savePath . '/' . $newName;
if (! move_uploaded_file ( $fileInfo ['tmp_name'], $desPath )) {
$info ['mes'] = $fileInfo ['name'] . "上传失败";
}
$info ['mes'] = "上传成功";
$info ['des'] = $desPath;
return $info;
} else {
switch ($fileInfo ['error']) {
case 1 :
$info ['mes'] = "上传文件大小超出了php配置文件upload_max_filesize的值";
break;
case 2 :
$info ['mes'] = "上传文件大小超出了HTML表单MAX_FILE_SIZE的值";
break;
case 3 :
$info ['mes'] = "文件只有部分被上传";
break;
case 4 :
$info ['mes'] = "没有选择任何文件";
break;
case 6 :
$info ['mes'] = "找不到临时文件夹";
break;
case 7 :
$info ['mes'] = "文件写入失败";
break;
}
return $info;
}
}
我看了下 匹配错误那没问题啊
没有错误输出是吗?
^-^...
输出页面我是这样写的 但是为什么判断不到错误 请老师指教下 谢谢!
require_once 'upload.func.php';
foreach ( getFileInfo() as $fileInfo){
$info=uploadFile($fileInfo);
echo $info['mes'].'<br/>';
}
PHP实现文件上传与下载
43735 学习 · 328 问题
相似问题