博主信息
博文 32
粉丝 2
评论 0
访问量 37776
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
单文件上传与批量文件上传
简行
原创
960人浏览过

一.实现单文件上传,要求自定义异常类来处理常见错误

代码部分:

  1. <?php
  2. class upException extends Exception{
  3. public function __toString()
  4. {
  5. return <<<UPE
  6. <table>
  7. <tr>
  8. <td>错误编号</td>
  9. <td>错误信息</td>
  10. <td>错误文件</td>
  11. <td>错误行号</td>
  12. </tr>
  13. <tr>
  14. <td>$this->code</td>
  15. <td>$this->message</td>
  16. <td>$this->file</td>
  17. <td>$this->line</td>
  18. </tr>
  19. </table>
  20. <style>
  21. table{text-align: center;border: 2px solid #000;border-collapse: collapse;}
  22. td { border:1px solid black;padding: 10px;}
  23. tr:first-of-type {background-color:#eee;}
  24. tr:last-of-type td {color:lightsalmon;background-color:lightskyblue;}
  25. </style>
  26. UPE;
  27. }
  28. }
  29. try{
  30. //
  31. $fileNanme = $_FILES['my_file']['name'] ?? null;
  32. $errorcode = $_FILES['my_file']['error'];
  33. if($errorcode > UPLOAD_ERR_OK){
  34. switch($errorcode){
  35. case UPLOAD_ERR_INI_SIZE:
  36. throw new upException("文件超过`php.ini`中`upload_max_filesize`值",1);
  37. break;
  38. case UPLOAD_ERR_FORM_SIZE:
  39. throw new upException("文件大小超过表单中`MAX_FILE_SIZE`指定的值",2);
  40. break;
  41. case UPLOAD_ERR_PARTIAL:
  42. throw new upException("文件只有部分被上传",3);
  43. break;
  44. case UPLOAD_ERR_NO_FILE:
  45. throw new upException("没有文件被上传",4);
  46. break;
  47. case UPLOAD_ERR_NO_TMP_DIR:
  48. throw new upException("找不到临时文件夹",6);
  49. break;
  50. case UPLOAD_ERR_CANT_WRITE:
  51. throw new upException("文件写入失败",7);
  52. break;
  53. default:
  54. // 测试时建议关掉default: 避免误报影响
  55. throw new upException('未知错误', 111);
  56. }
  57. }
  58. $fileType = $_FILES['my_file']['type'] ?? null;
  59. $type = strstr($fileType, '/', true); // "image"
  60. if (!is_null($fileType)) {
  61. if ($type !== 'image') throw new upException('文件类型错误',120);
  62. }
  63. $fileTmpNanme = $_FILES['my_file']['tmp_name'] ?? null;
  64. // is_uploaded_file():检查指定的文件是否是通过 HTTP POST 上传的
  65. // echo $fileTmpNanme ;
  66. // echo is_uploaded_file($fileTmpNanme);
  67. if($fileTmpNanme && is_uploaded_file($fileTmpNanme)){
  68. //文件名
  69. $orgename = $_FILES['my_file']['name'] ?? null;
  70. $ftype = strstr($orgename, '.');
  71. //文件保存地址
  72. $upload = "./upload";
  73. // is_dir() 函数检查指定的文件是否是目录。
  74. if(!is_dir($upload)){
  75. mkdir($upload,0777,true);
  76. }
  77. //保存到服务器的文件地址
  78. $upfile = $upload."/".time()."dddd".$ftype;
  79. if(move_uploaded_file($fileTmpNanme,$upfile)){
  80. echo '<img src="'.$upfile.'" alt="上传的图片" style="width: 100px;">
  81. <span style="color: rosybrown;">图片上传成功</span>';
  82. }else{
  83. echo "上传失败";
  84. }
  85. }
  86. }catch(upException $up){
  87. echo $up;
  88. }
  89. ?>
  90. <!DOCTYPE html>
  91. <html lang="en">
  92. <head>
  93. <meta charset="UTF-8">
  94. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  95. <title>单文件上传</title>
  96. </head>
  97. <body>
  98. <form action="" method="POST"" enctype="multipart/form-data">
  99. <fieldset>
  100. <legend>单文件上传</legend>
  101. <!-- 前端设置上传文件大小,必须写到input:file之前 -->
  102. <input type="hidden" name="MAX_FILE_SIZE" value="38000">
  103. <input type="file" name="my_file">
  104. <input type="submit" value="提交">
  105. </fieldset>
  106. </form>
  107. </body>
  108. </html>

文件位置截图:

前端截图:

二.文件批量上传

代码部分:

  1. <?php
  2. // printf('<pre>%s</pre>', print_r($_FILES, true));
  3. class upException extends Exception{
  4. public function __toString()
  5. {
  6. return <<<UPE
  7. <table>
  8. <tr>
  9. <td>错误编号</td>
  10. <td>错误信息</td>
  11. <td>错误文件</td>
  12. <td>错误行号</td>
  13. </tr>
  14. <tr>
  15. <td>$this->code</td>
  16. <td>$this->message</td>
  17. <td>$this->file</td>
  18. <td>$this->line</td>
  19. </tr>
  20. </table>
  21. <style>
  22. table{text-align: center;border: 2px solid #000;border-collapse: collapse;}
  23. td { border:1px solid black;padding: 10px;}
  24. tr:first-of-type {background-color:#eee;}
  25. tr:last-of-type td {color:lightsalmon;background-color:lightskyblue;}
  26. </style>
  27. UPE;
  28. }
  29. }
  30. $num = 0;
  31. foreach($_FILES as $file){
  32. try{
  33. $fileNanme = $file['name'] ?? null;
  34. $errorcode = $file['error'];
  35. if($errorcode > UPLOAD_ERR_OK){
  36. switch($errorcode){
  37. case UPLOAD_ERR_INI_SIZE:
  38. throw new upException("文件超过`php.ini`中`upload_max_filesize`值",1);
  39. break;
  40. case UPLOAD_ERR_FORM_SIZE:
  41. throw new upException("文件大小超过表单中`MAX_FILE_SIZE`指定的值",2);
  42. break;
  43. case UPLOAD_ERR_PARTIAL:
  44. throw new upException("文件只有部分被上传",3);
  45. break;
  46. case UPLOAD_ERR_NO_FILE:
  47. throw new upException("没有文件被上传",4);
  48. break;
  49. case UPLOAD_ERR_NO_TMP_DIR:
  50. throw new upException("找不到临时文件夹",6);
  51. break;
  52. case UPLOAD_ERR_CANT_WRITE:
  53. throw new upException("文件写入失败",7);
  54. break;
  55. default:
  56. // 测试时建议关掉default: 避免误报影响
  57. throw new upException('未知错误', 111);
  58. }
  59. }
  60. $fileType = $file['type'] ?? null;
  61. $type = strstr($fileType, '/', true); // "image"
  62. if (!is_null($fileType)) {
  63. if ($type !== 'image') throw new upException('文件类型错误',120);
  64. }
  65. $fileTmpNanme = $file['tmp_name'] ?? null;
  66. // is_uploaded_file():检查指定的文件是否是通过 HTTP POST 上传的
  67. if($fileTmpNanme && is_uploaded_file($fileTmpNanme)){
  68. //文件名
  69. $orgename = $file['name'] ?? null;
  70. $ftype = strstr($orgename, '.');
  71. //文件保存地址
  72. $upload = "./uploads";
  73. // is_dir() 函数检查指定的文件是否是目录。
  74. if(!is_dir($upload)){
  75. mkdir($upload,0777,true);
  76. }
  77. //保存到服务器的文件地址
  78. $upfile = $upload."/one".time().$num.$ftype;
  79. if(move_uploaded_file($fileTmpNanme,$upfile)){
  80. $num++;
  81. echo '<img src="'.$upfile.'" alt="上传的图片" style="width: 100px;">
  82. <span style="color: rosybrown;">图片上传成功</span>';
  83. }else{
  84. echo "上传失败";
  85. }
  86. }
  87. // else{
  88. // throw new upException("文件上传方式不正确",119);
  89. // }
  90. }catch(upException $up){
  91. echo $up;
  92. }
  93. }
  94. ?>
  95. <!DOCTYPE html>
  96. <html lang="en">
  97. <head>
  98. <meta charset="UTF-8">
  99. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  100. <title>单文件上传</title>
  101. </head>
  102. <body>
  103. <form action="" method="POST"" enctype="multipart/form-data">
  104. <fieldset>
  105. <legend>单文件上传</legend>
  106. <!-- 前端设置上传文件大小,必须写到input:file之前 -->
  107. <!-- <input type="hidden" name="MAX_FILE_SIZE" value="38000"> -->
  108. <input type="file" name="pic1">
  109. <input type="file" name="pic2">
  110. <input type="file" name="pic3">
  111. <input type="submit" value="提交">
  112. </fieldset>
  113. </form>
  114. </body>
  115. </html>

文件位置截图:

前端截图:

批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:文件上传有许多要注意的地方
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学