单文件上传
<?php
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('上传文件的大小超过了 HTML 表单中 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_NO_TMP_DIR:
throw new UploadException('找不到临时文件夹', 6);
break;
case UPLOAD_ERR_CANT_WRITE:
throw new UploadException('文件写入失败', 7);
break;
default:
// 测试时建议关掉default: 避免误报影响
throw new UploadException('未知类型错误', 8);
}
}
// 判断文件类型
$fileType = $_FILES['pic']['type'];
if (strstr($fileType, '/', true) !== 'image') throw new UploadException('文件类型错误',9);
$tempName =$_FILES['pic']['tmp_name'];
if (is_uploaded_file($tempName)){
// 将文件从临时目录 移动到用户自定义的目标目录中
// 临时文件名
echo $originalFileName=$_FILES['pic']['name'];
echo '<br>';
// 目录文件名
echo $destFileName = 'uploads/'.md5(time().mt_rand(1,1000)).strstr($originalFileName, '.');
if (move_uploaded_file($tempName,$destFileName)){
echo "<p>$originalFileName: 上传成功~~</p>";
// 预览
echo "<img src='{$destFileName}' width='200'>";
}else{
echo "<p>$originalFileName: 没有成功~~</p>";
}
}
}catch (UploadException $e){
echo $e;
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<legend>单文件上传 </legend>
<input type="hidden" name="MAX_FILE_SIZE" VALUE="50000">
<input type="file" name="pic">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
多文件上传
<?php
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('上传文件的大小超过了 HTML 表单中 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_NO_TMP_DIR:
throw new UploadException('找不到临时文件夹', 6);
break;
case UPLOAD_ERR_CANT_WRITE:
throw new UploadException('文件写入失败', 7);
break;
default:
// 测试时建议关掉default: 避免误报影响
throw new UploadException('未知类型错误', 8);
}
}
foreach ($_FILES as $file){
// print_r($file);
if ($file['error'] ===0){
$destFile='uploads/'.md5(time().mt_rand(1,1000)).strstr($file['name'], '.');;
if (move_uploaded_file($file['tmp_name'],$destFile)){
echo "<p>$destFile: 上传成功~~</p>";
// 预览
echo "<img src='{$destFile}' width='200'>";
}else{
echo "<p>$destFile: 没有成功~~</p>";
}
}
}
}catch (UploadException $e){
echo $e;
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<legend>单文件上传 :异常处理</legend>
<input type="hidden" name="MAX_FILE_SIZE" VALUE="50000">
<input type="file" name="pic1">
<input type="file" name="pic2">
<input type="file" name="pic3">
<button>上传</button>
<!-- <input type="button" value="上传">-->
</fieldset>
</form>
</body>
</html>
批量上传
<input type="file" name="pic[]" multiple>
多选并上传文件时候 天添加multiple 这样可以选多个文件并且上传
```
<?php
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(‘上传文件的大小超过了 HTML 表单中 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_NO_TMP_DIR:
throw new UploadException(‘找不到临时文件夹’, 6);
break;
case UPLOAD_ERR_CANT_WRITE:
throw new UploadException(‘文件写入失败’, 7);
break;
default:
// 测试时建议关掉default: 避免误报影响
throw new UploadException(‘未知类型错误’, 8);
}
}
// 判断文件类型
// $fileType = $_FILES[‘pic’][‘type’];
// if (strstr($fileType, ‘/‘, true) !== ‘image’) throw new UploadException(‘文件类型错误’,9);
if ($_FILES[‘pic’]){
foreach ($_FILES[‘pic’][‘error’] as $key=>$error) {
print_r($error);
// 只要判断 error===0
if ($error === 0){
//临时文件的名字
$tmpFileName=$_FILES[‘pic’][‘tmp_name’][$key];
//原始文件名
$originalFileName = $_FILES[‘pic’][‘name’][$key];
//目标文件名
$destFileName = 'uploads/'.md5(time().mt_rand(0,10000)).strstr($originalFileName,',');
// 移动文件
move_uploaded_file($tmpFileName,$destFileName);
// 预览
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, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<legend>单文件上传 :异常处理</legend>
<input type="hidden" name="MAX_FILE_SIZE" VALUE="50000">
<input type="file" name="pic[]">
<input type="file" name="pic[]">
<input type="file" name="pic[]">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
```