文件上传
1.单文件上传
<?php
// $_FILES
printf('<pre>%s</pre>', print_r($_FILES, true));
//自定义异常类
// 自定义上传异常类
class UploadException extends Exception
{
// 在异常子类中,可以访问并重写Exception中的四个属性,通过__toString()格式化异常输出信息
public function __toString()
{
return <<< UPLOAD
<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: coral;}
</style>
<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>
UPLOAD;
}
}
///////////////////////////////////////
try {
//上传出错的代码
$errorCode = $_FILES['my_pic']['error'];
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('文件大小超过表单中`MAX_FILE_SIZE`指定的值', 2);
break;
case UPLOAD_ERR_PARTIAL:
throw new UploadException('文件只有部分被上', 3);
break;
case UPLOAD_ERR_NO_FILE:
throw new UploadException('没有文件上传', 4);
break;
case UPLOAD_ERR_CANT_WRITE:
throw new UploadException('找不到临时文件夹', 6);
break;
case UPLOAD_ERR_CANT_WRITE:
throw new UploadException('文件写入失败', 7);
break;
default:
throw new UploadException('未知类型错误', 8);
break;
}
}
$fileType = $_FILES['my_pic']['type'];
// var_dump(strstr($fileType, '/', true));
if (isset($fileType) && strstr($fileType, '/', true) !== 'image') throw new UploadException('文件类型错误', 9);
//将文件从临时目录移动到用户自定义的目标目录中
$tmpFileName = $_FILES['my_pic']['tmp_name'];
if (is_uploaded_file($tmpFileName)) {
//获取原始文件名
$originalFileName = $_FILES['my_pic']['name'];
//生成目标文件名
$destFileName = 'uploads/' . md5(mt_rand(1, 1000) . time()) . strstr($originalFileName, '.');
if (move_uploaded_file($tmpFileName, $destFileName)) {
echo "<p>$originalFileName:文件上传成功~~</p>";
echo "<img src='{$destFileName}' width='200'>";
}
}
} catch (UploadException $e) {
echo $e;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件上传变量$_FILES</title>
</head>
<body>
<hr>
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<legend>单文件上传:文件移动</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="3000000">
<input type="file" name="my_pic" id="">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
2.多文件上传
<?php
// $_FILES
printf('<pre>%s</pre>', print_r($_FILES, true));
$files = array_pop($_FILES);
if (is_array($files)) {
foreach ($files['error'] as $key => $error) {
if ($error === UPLOAD_ERR_OK) {
//文件类型检查
$fileType = $files['type'][$key];
$types = ['image/jpeg', 'image/png', 'image/gif'];
if (in_array($fileType, $types)) {
$originalFilName = $files['name'][$key];
$tmpFileName = $files['tmp_name'][$key];
//目标文件名称
$destFileName = 'uploads/' . md5(mt_rand(1, 1000) . time()) . strstr($originalFilName, '.', true);
move_uploaded_file($tmpFileName, $destFileName);
echo "<img src='{$destFileName}' width='200'>";
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件上传变量$_FILES</title>
</head>
<body>
<hr>
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<legend>多文件上传:逐个上传(二)</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="3000000">
<input type="file" name="my_pic[]" id="">
<input type="file" name="my_pic[]" id="">
<input type="file" name="my_pic[]" id="">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
3.批量文件上传
<?php
// $_FILES
printf('<pre>%s</pre>', print_r($_FILES, true));
$files = array_pop($_FILES);
if (is_array($files)) {
foreach ($files['error'] as $key => $error) {
if ($error === UPLOAD_ERR_OK) {
$originalFileName = $files['name'][$key];
$tmpFileName = $files['tmp_name'][$key];
$destFileName = 'uploads/' . md5(mt_rand(1, 1000) . time()) . strstr($originalFileName, '.', true);
move_uploaded_file($tmpFileName, $destFileName);
echo "<img src='{$destFileName}' width='200'>";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件上传变量$_FILES</title>
</head>
<body>
<hr>
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<legend>单文件上传:文件移动</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="3000000">
<input type="file" name="my_pic[]" id="" multiple>
<button>上传</button>
</fieldset>
</form>
</body>
</html>
总结:文件上传最主要的就是要用到超全局变量$_FILE,所有的文件上传信息都在这个数组里面。文件上传有7中类型编码,没有5,从0-7分别对应的是“文件上传成功”、“文件超过php.ini
中upload_max_filesize
值”、“文件超过表单中MAX_FILE_SIZE指定的值”、“文件只有部分上传”、“文件没有被上传”、“找不到临时文件夹”、“文件写入失败”。判定时也可以用对应的常量来判定。