PHP文件上传函数封装
作业标题:PHP文件上传函数封装
作业内容:根据需求封装文件上传限制函数: 1. 文件大小不超过5M 2. 后缀只能是[‘jpg’,’jpeg’,’png’,’wbmp’,’gif’] 3. 检查图片合法性 4.为每一张图片处理散列名称 5. 目标目录不存在,创建目录 6.设置错误日志接收系统错误,而不是将错误显示到页面上
首页demo1.php代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件上传</title>
</head>
<body>
<!-- application/x-www-urlencoded 不合适二进制数据或者包含ASCII字符的传输 -->
<form action="upload3.php" method="post" enctype="multipart/form-data">
<!-- <fieldset>
<legend>
单文件上传
</legend>
<label for="my_file"></label>
<input type="file" name="my_file" id="my_file">
<button>上传</button>
</fieldset> -->
<fieldset>
<legend>
多文件上传
</legend>
<label for="my_file"></label>
<input type="file" name="my_file[]" id="my_file" multiple>
<button>上传</button>
</fieldset>
</form>
</body>
</html>
- upload代码
<?php
ini_set('display_errors','Off'); //页面不现实任何错误
ini_set('error_log','./error.log');
require 'common1.php';
$files = upload($_FILES['my_file']);
foreach ($files as $fileInfo)
{
$res = uploadFile($fileInfo);
echo "<p>{$res['info']}</p>";
echo "<img style='border-radius:50%;width:100px';height:50% src='{$res['fileRealPath']}'>";
$uploadFiles[] = $res['fileRealPath'];
}
printf('<pre>%s</pre>',print_r($uploadFiles,true));
echo json_encode($uploadFiles);
- 封装函数common.php
<?php
ini_set('display_errors','Off'); //页面不现实任何错误
ini_set('error_log','./error.log');
//文件上传函数
function uploadFile($fileInfo,$uploadPath='./storage',$flag=true,$allowExt=['jpg','png','wbmp','gif','jpeg'],$maxSize=5242880)
{
if($fileInfo['error'] == 0)
{
// 后端设置文件的类型
$arr = explode('.',$fileInfo['name']);
$ext = end($arr);
$prefix = array_shift($arr);
if(!in_array($ext,$allowExt))
{
$res['error']='非法的文件类型';
}
if($fileInfo['size'] > $maxSize)
{
$res['error']='文件大小超过限制的最大值';
}
if($flag)
{
//检测图片是否合法
if(!getimageSize($fileInfo['tmp_name']))
{
$res['error']='不是真实图片,get out~';
}
}
if(!is_uploaded_file($fileInfo['tmp_name']))
{
$res['error'] ='上传方式错误:请使用http post方式上传';
}
if(!empty($res)) return;
// -----------------------------------------------------------------------
// copy(),move_uploaded_file()可以将文件从临时目录移动到指定路径
if(!file_exists($uploadPath))
{
mkdir($uploadPath,0770,true);
chmod($uploadPath,0770);
}
// 根据$prefix time() 生成唯一图片地址
$des = $uploadPath.'/'.md5($prefix.time()).'.'.$ext;
$result = move_uploaded_file($fileInfo['tmp_name'],$des);
if(!$result)
{
$res['error'] = '文件移动失败';
}else{
$res['info'] = $fileInfo['name'] . '上传成功';
$res['fileRealPath'] = $des;
}
return $res;
}else{
switch($error) :
case 1:
$res['error'] = '文件超过`php.ini`中`upload_max_filesize`值<br>';
break;
case 2:
$res['error'] = '文件大小超过表单中`MAX_FILE_SIZE`指定的值<br>';
break;
case 3:
$res['error'] = '文件只有部分被上传<br>';
break;
case 4:
$res['error'] = '没有文件被上传<br>';
break;
case 6:
$res['error'] = '找不到临时文件夹<br>';
break;
case 6:
$res['error'] = '文件写入失败<br>';
break;
default:
$res['error'] = '系统错误<br>';
break;
endswitch;
return $res;
}
}
function upload($fileInfo)
{
foreach($fileInfo as $k=>$v)
{
foreach($v as $kk=>$vv)
{
if($kk == $kk )
{
$files[$kk][$k] = $vv;
}
}
}
return $files;
}