单文件上传
- 表单上传
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<legend>单文件上传</legend>
<!-- 前端设置上传文件的大小:字节 -->
<!-- <input type="hidden" name="MAX_FILE_SIZE" value="5600"> -->
<input type="file" name="my_pic" id="">
<button>上传</button>
</fieldset>
</form>
- 查看上传信息
// $_FILES: 文件上传变量,超全局的变量数组, 保存着所有与文件上传的相关信息
printf('<pre>%s</pre>', print_r($_FILES, true));
自定义异常类
- 自定义文件上传的异常类
// 自定义文件上传的异类处理类
class UploadException extends Exception
{
public function __toString()
{
return <<<UPLOAD
<table>
<tr>
<td>编号</td>
<td>信息</td>
<td>文件</td>
<td>行号</td>
</tr>
<tr>
<td>$this->code</td>
<td>$this->message</td>
<td>$this->file</td>
<td>$this->line</td>
</tr>
</table>
<style>
table {border-collapse: collapse;border:1px solid black;text-align: center;}
td {border:1px solid black;padding: 5px;}
tr:first-of-type {background-color:#eee;}
tr:last-of-type td {color: red;}
</style>
UPLOAD;
}
}
调用异常类
try
{
// 1.对错误代码的判断
$errorCode = $_FILES['my_pic']['error'] ?? null;
// 使用系统常量进行判断
if($errorCode>UPLOAD_ERR_OK){
switch ($errorCode){
case UPLOAD_ERR_INI_SIZE:
throw new UploadException('上传的文件超过了php.ini 中 upload_max_filesize 选项限制的值',1);
break;
case UPLOAD_ERR_FORM_SIZE:
throw new UploadException('上传的文件超出了表单中设置的大小',2);
break;
case UPLOAD_ERR_PARTIAL:
throw new UploadException('文件只有部分被上传', 3);
break;
case UPLOAD_ERR_NO_FILE:
throw new UploadException('没有文件被上传', 4);
break;
case UPLOAD_ERR_NO_TMP_DIR:
throw new UploadException('找不到临时文件夹', 5);
break;
case UPLOAD_ERR_CANT_WRITE:
throw new UploadException('文件写入失败', 6);
break;
default:
// 测试时建议关掉default: 避免误报影响
throw new UploadException('未知类型错误', 7);
}
}
// 2.判断文件类型是否正确
$fileType = $_FILES['my_pic']['type'] ?? null;
$type = strstr($fileType,'/',true);
if(!is_null($fileType)){
if($type!=='image') throw new UploadException('上传类型错误',8);
}
}catch(UploadException $e){
echo $e;
}
- 单文件(图片上传)
- 先准备一个存放上传文件的文件
- 把临时文件上传到目标目录
// 判断上传方式是否正确?
// 临时文件名
$tmplFileName = $_FILES['my_pic']['tmp_name'] ?? null;
// 1. 首先临时文件是存在的,并且必须是通过POST上传的
if ($tmplFileName && is_uploaded_file($tmplFileName)) {
// 2. 将文件从临时目录移动到目标目录中: uploads/
// move_uploaded_file(临时文件, 目标文件名)
// 原始文件名
$originalFileName = $_FILES['my_pic']['name'] ?? null;
echo strstr($originalFileName, '.');
// 目标文件名
$destFileName = 'uploads/' . md5(time(). mt_rand(1,1000)) .strstr($originalFileName, '.');
if (move_uploaded_file($tmplFileName, $destFileName)) {
echo '<p>'. $_FILES['my_pic']['name'].': 上传成功</p>';
// 预览图片
echo "<img src='{$destFileName}' width='200'>";
}
}
多文件上传
文件批量上传中缺失的异常处理, 文件类型处理等操作
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<legend>批量上传文件</legend>
<input type="file" name="my_pic1" id="">
<input type="file" name="my_pic2" id="">
<input type="file" name="my_pic3" id="">
<button>上传</button>
</fieldset>
</form>
foreach ($_FILES as $k=>$file) {
// echo $k;
try
{
// 1.对错误代码的判断
$errorCode = $_FILES[$k]['error'] ?? null;
// 使用系统常量进行判断
if($errorCode>UPLOAD_ERR_OK){
switch ($errorCode){
case UPLOAD_ERR_INI_SIZE:
throw new UploadException('上传的文件超过了php.ini 中 upload_max_filesize 选项限制的值',1);
break;
case UPLOAD_ERR_FORM_SIZE:
throw new UploadException('上传的文件超出了表单中设置的大小',2);
break;
case UPLOAD_ERR_PARTIAL:
throw new UploadException('文件只有部分被上传', 3);
break;
case UPLOAD_ERR_NO_FILE:
throw new UploadException('没有文件被上传', 4);
break;
case UPLOAD_ERR_NO_TMP_DIR:
throw new UploadException('找不到临时文件夹', 5);
break;
case UPLOAD_ERR_CANT_WRITE:
throw new UploadException('文件写入失败', 6);
break;
default:
// 测试时建议关掉default: 避免误报影响
throw new UploadException('未知类型错误', 7);
}
}
// 2.判断文件类型是否正确
$fileType = $_FILES[$k]['type'] ?? null;
$type = strstr($fileType,'/',true);
if(!is_null($fileType)){
if($type!=='image') throw new UploadException('上传类型错误',8);
}
}catch(UploadException $e){
echo $e;
}
}
- 文件批量上传
if($file['error']===0){
$destFileName = 'uploads/' . md5(time().mt_rand(1,1000)) . strstr($file['name'],'.');
if(move_uploaded_file($file['tmp_name'],$destFileName)){
echo $file['name'].':上传成功!'.'<hr>';
// 预览图片
echo "<img src='{$destFileName}' width='200'>";
}
}
*封装一个文件上传类,可同时处理单文件与多文件上传(还在进一步学习)