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'];
// 判断 出错代码是否大于0
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['my_pic']['type'];
if (strstr($fileType, '/', true) == ''){
echo '请选择要上传的文件';
}else{
if (strstr($fileType, '/', true) !== 'image') throw new UploadException('文件类型错误',9);
}
}
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">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
效果:
上传效果:
错误提示:
2.多文件上传
代码
<?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'];
// 判断 出错代码是否大于0
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['my_pic']['type']['0'];
if (@strstr($fileType, '/', true) == ''){
echo '请选择要上传的文件';
}else{
if (strstr($fileType, '/', true) !== 'image') throw new UploadException('文件类型错误',9);
}
if ($_FILES['my_pic'])
// 遍历error 健和值
foreach ($_FILES['my_pic']['error'] as $key => $error) {
// 判断 error 是否=== 0
if ($error === UPLOAD_ERR_OK) {
// 获取临时文件名
$tmpFileName = $_FILES['my_pic']['tmp_name'][$key];
// 获取原始文件名
$originalFileName = $_FILES['my_pic']['name'][$key];
// 要转移动目录和文件名命名
$destFileName = 'uploads/'. $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, 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">
<!-- 将name属性值能数组的形式提供 -->
<input type="file" name="my_pic[]">
<input type="file" name="my_pic[]">
<input type="file" name="my_pic[]">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
上传效果:
错误信息:
没有处理好的地方:
3. 批量上传文件
代码
<?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']['0'];
// 判断 出错代码是否大于0
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['my_pic']['type']['0'];
if (@strstr($fileType, '/', true) == ''){
echo '请选择要上传的文件';
}else{
if (strstr($fileType, '/', true) !== 'image') throw new UploadException('文件类型错误',9);
}
if ($_FILES['my_pic'])
// 遍历error 健和值
foreach ($_FILES['my_pic']['error'] as $key => $error) {
// 判断 error 是否=== 0
if ($error === UPLOAD_ERR_OK) {
// 获取临时文件名
$tmpFileName = $_FILES['my_pic']['tmp_name'][$key];
// 获取原始文件名
$originalFileName = $_FILES['my_pic']['name'][$key];
// 要转移动目录和文件名命名
$destFileName = 'uploads/'. $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, 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">
<!-- 将name属性值能数组的形式提供 -->
<input type="file" name="my_pic[]" multiple>
<button>上传</button>
</fieldset>
</form>
</body>
</html>
选择效果:
上传效果:
总结:学习了文件上传的流程步骤以及用到哪些函数,但是不怎么懂怎么封装类。