博客列表 >单文件上传和多文件上传和批量上传

单文件上传和多文件上传和批量上传

default
default原创
2020年05月13日 16:31:531251浏览

单文件上传

  1. <?php
  2. printf('<pre>%s</pre>',print_r($_FILES,true));
  3. //自定义上传类
  4. class UploadException extends Exception{
  5. // 在异常类中可以访问并且中写 Exception 中的四个属性,通过 __toString()格式化异常输出信息
  6. public function __toString()
  7. {
  8. return <<< UPLOAD
  9. <style>
  10. table {border-collapse: collapse;border:1px solid black;text-align: center;}
  11. td {border:1px solid black;padding: 5px;}
  12. tr:first-of-type {background-color:#eee;}
  13. tr:last-of-type td {color: coral;}
  14. </style>
  15. <table>
  16. <tr>
  17. <td>代码</td><td>信息</td><td>文件</td><td>行号</td>
  18. </tr>
  19. <tr>
  20. <td>$this->code</td><td>$this->message</td><td>$this->file</td><td>$this->line</td>
  21. </tr>
  22. </table>
  23. UPLOAD;
  24. }
  25. }
  26. ///////////////////////////////////////////////////////
  27. try{
  28. // 上传出错的代码
  29. $errorCode = $_FILES['my_pic']['error'];
  30. if ($errorCode > UPLOAD_ERR_OK) {
  31. switch ($errorCode) {
  32. case UPLOAD_ERR_INI_SIZE:
  33. throw new UploadException('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值', 1);
  34. break;
  35. case UPLOAD_ERR_FORM_SIZE:
  36. throw new UploadException('上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值', 2);
  37. break;
  38. case UPLOAD_ERR_PARTIAL:
  39. throw new UploadException('文件只有部分被上传', 3);
  40. break;
  41. case UPLOAD_ERR_NO_FILE:
  42. throw new UploadException('没有文件被上传', 4);
  43. break;
  44. case UPLOAD_ERR_NO_TMP_DIR:
  45. throw new UploadException('找不到临时文件夹', 6);
  46. break;
  47. case UPLOAD_ERR_CANT_WRITE:
  48. throw new UploadException('文件写入失败', 7);
  49. break;
  50. default:
  51. // 测试时建议关掉default: 避免误报影响
  52. throw new UploadException('未知类型错误', 8);
  53. }
  54. }
  55. // 判断文件类型
  56. $fileType = $_FILES['pic']['type'];
  57. if (strstr($fileType, '/', true) !== 'image') throw new UploadException('文件类型错误',9);
  58. $tempName =$_FILES['pic']['tmp_name'];
  59. if (is_uploaded_file($tempName)){
  60. // 将文件从临时目录 移动到用户自定义的目标目录中
  61. // 临时文件名
  62. echo $originalFileName=$_FILES['pic']['name'];
  63. echo '<br>';
  64. // 目录文件名
  65. echo $destFileName = 'uploads/'.md5(time().mt_rand(1,1000)).strstr($originalFileName, '.');
  66. if (move_uploaded_file($tempName,$destFileName)){
  67. echo "<p>$originalFileName: 上传成功~~</p>";
  68. // 预览
  69. echo "<img src='{$destFileName}' width='200'>";
  70. }else{
  71. echo "<p>$originalFileName: 没有成功~~</p>";
  72. }
  73. }
  74. }catch (UploadException $e){
  75. echo $e;
  76. }
  77. ?>
  78. <!doctype html>
  79. <html lang="en">
  80. <head>
  81. <meta charset="UTF-8">
  82. <meta name="viewport"
  83. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  84. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  85. <title>Document</title>
  86. </head>
  87. <body>
  88. <form action="" method="post" enctype="multipart/form-data">
  89. <fieldset>
  90. <legend>单文件上传 </legend>
  91. <input type="hidden" name="MAX_FILE_SIZE" VALUE="50000">
  92. <input type="file" name="pic">
  93. <button>上传</button>
  94. </fieldset>
  95. </form>
  96. </body>
  97. </html>

多文件上传

  1. <?php
  2. printf('<pre>%s</pre>',print_r($_FILES,true));
  3. //自定义上传类
  4. class UploadException extends Exception{
  5. // 在异常类中可以访问并且中写 Exception 中的四个属性,通过 __toString()格式化异常输出信息
  6. public function __toString()
  7. {
  8. return <<< UPLOAD
  9. <style>
  10. table {border-collapse: collapse;border:1px solid black;text-align: center;}
  11. td {border:1px solid black;padding: 5px;}
  12. tr:first-of-type {background-color:#eee;}
  13. tr:last-of-type td {color: coral;}
  14. </style>
  15. <table>
  16. <tr>
  17. <td>代码</td><td>信息</td><td>文件</td><td>行号</td>
  18. </tr>
  19. <tr>
  20. <td>$this->code</td><td>$this->message</td><td>$this->file</td><td>$this->line</td>
  21. </tr>
  22. </table>
  23. UPLOAD;
  24. }
  25. }
  26. ///////////////////////////////////////////////////////
  27. try{
  28. // 上传出错的代码
  29. $errorCode = $_FILES['my_pic']['error'];
  30. if ($errorCode > UPLOAD_ERR_OK) {
  31. switch ($errorCode) {
  32. case UPLOAD_ERR_INI_SIZE:
  33. throw new UploadException('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值', 1);
  34. break;
  35. case UPLOAD_ERR_FORM_SIZE:
  36. throw new UploadException('上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值', 2);
  37. break;
  38. case UPLOAD_ERR_PARTIAL:
  39. throw new UploadException('文件只有部分被上传', 3);
  40. break;
  41. case UPLOAD_ERR_NO_FILE:
  42. throw new UploadException('没有文件被上传', 4);
  43. break;
  44. case UPLOAD_ERR_NO_TMP_DIR:
  45. throw new UploadException('找不到临时文件夹', 6);
  46. break;
  47. case UPLOAD_ERR_CANT_WRITE:
  48. throw new UploadException('文件写入失败', 7);
  49. break;
  50. default:
  51. // 测试时建议关掉default: 避免误报影响
  52. throw new UploadException('未知类型错误', 8);
  53. }
  54. }
  55. foreach ($_FILES as $file){
  56. // print_r($file);
  57. if ($file['error'] ===0){
  58. $destFile='uploads/'.md5(time().mt_rand(1,1000)).strstr($file['name'], '.');;
  59. if (move_uploaded_file($file['tmp_name'],$destFile)){
  60. echo "<p>$destFile: 上传成功~~</p>";
  61. // 预览
  62. echo "<img src='{$destFile}' width='200'>";
  63. }else{
  64. echo "<p>$destFile: 没有成功~~</p>";
  65. }
  66. }
  67. }
  68. }catch (UploadException $e){
  69. echo $e;
  70. }
  71. ?>
  72. <!doctype html>
  73. <html lang="en">
  74. <head>
  75. <meta charset="UTF-8">
  76. <meta name="viewport"
  77. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  78. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  79. <title>Document</title>
  80. </head>
  81. <body>
  82. <form action="" method="post" enctype="multipart/form-data">
  83. <fieldset>
  84. <legend>单文件上传 :异常处理</legend>
  85. <input type="hidden" name="MAX_FILE_SIZE" VALUE="50000">
  86. <input type="file" name="pic1">
  87. <input type="file" name="pic2">
  88. <input type="file" name="pic3">
  89. <button>上传</button>
  90. <!-- <input type="button" value="上传">-->
  91. </fieldset>
  92. </form>
  93. </body>
  94. </html>

批量上传

  • <input type="file" name="pic[]" multiple> 多选并上传文件时候 天添加multiple 这样可以选多个文件并且上传
    ```
    <?php

printf(‘<pre>%s</pre>‘,print_r($_FILES,true));

//自定义上传类
class UploadException extends Exception{
// 在异常类中可以访问并且中写 Exception 中的四个属性,通过 __toString()格式化异常输出信息

  1. public function __toString()
  2. {
  3. return <<< UPLOAD
  4. <style>
  5. table {border-collapse: collapse;border:1px solid black;text-align: center;}
  6. td {border:1px solid black;padding: 5px;}
  7. tr:first-of-type {background-color:#eee;}
  8. tr:last-of-type td {color: coral;}
  9. </style>
  10. <table>
  11. <tr>
  12. <td>代码</td><td>信息</td><td>文件</td><td>行号</td>
  13. </tr>
  14. <tr>
  15. <td>$this->code</td><td>$this->message</td><td>$this->file</td><td>$this->line</td>
  16. </tr>
  17. </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);
}
}

  1. // 判断文件类型

// $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];

  1. //目标文件名
  2. $destFileName = 'uploads/'.md5(time().mt_rand(0,10000)).strstr($originalFileName,',');
  3. // 移动文件
  4. move_uploaded_file($tmpFileName,$destFileName);
  5. // 预览
  6. echo "<img src='{$destFileName}' width='200'>";
  7. }

}
};

}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>
```

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议