Blogger Information
Blog 39
fans 0
comment 0
visits 30629
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP:文件上传实战
Original
728 people have browsed it

单个文件上传

  1. <?php
  2. echo '超全局变量 $_FILES 数组的键值:';
  3. // $file = $_FILES;
  4. printf('<pre>%s</pre>',print_r($_FILES, true));
  5. echo '<hr>';
  6. $filesSize = $_FILES['my_pic']['size'];
  7. echo '允许的尽可能最大的文件上传(以字节为单位):' . ini_get('upload_max_filesize').'<hr>';
  8. if ($filesSize > 100000000 ) {
  9. echo '上传文件的大小超过最大限制!'.'<hr>';
  10. }else{
  11. echo '上传文件的大小小于最大限制!'.'<hr>';
  12. }
  13. if(strstr($_FILES['my_pic']['type'],'/',true) == 'image') {
  14. echo '上传的文件类型是image!'.'<hr>';
  15. }else{
  16. echo '上传的文件类型不是image!'.'<hr>';
  17. }
  18. if (is_uploaded_file($_FILES['my_pic']['tmp_name'])) echo $_FILES['my_pic']['name'] . '文件上传方式是合法的!'.'<hr>';
  19. // 自定义上传异常类
  20. class UploadException extends Exception
  21. {
  22. // 在异常子类中,可以访问并重写Exception中的四个属性,通过__toString()格式化异常输出信息
  23. public function __toString()
  24. {
  25. return <<< UPLOAD
  26. <style>
  27. table {border-collapse: collapse;border:1px solid black;text-align: center;}
  28. td {border:1px solid black;padding: 5px;}
  29. tr:first-of-type {background-color:#eee;}
  30. tr:last-of-type td {color: coral;}
  31. </style>
  32. <table>
  33. <tr><td>代码</td><td>信息</td><td>文件</td><td>行号</td></tr>
  34. <tr><td>$this->code</td><td>$this->message</td><td>$this->file</td><td>$this->line</td></tr>
  35. </table>
  36. UPLOAD;
  37. }
  38. }
  39. try{
  40. // 判断文件出错信息
  41. echo '文件状态 : ';
  42. $errcode = $_FILES['my_pic']['error'];
  43. switch($errcode){
  44. case '0':
  45. echo '文件上传成功!';
  46. echo '<hr>';
  47. break;
  48. case '1':
  49. throw new UploadException('文件超过`php.ini`中`upload_max_filesize`值!',1);
  50. echo '<hr>';
  51. break;
  52. case '2':
  53. throw new UploadException( '文件大小超过表单中`MAX_FILE_SIZE`指定的值!',2);
  54. echo '<hr>';
  55. break;
  56. case '3':
  57. throw new UploadException( '文件只有部分被上传!',3);
  58. echo '<hr>';
  59. break;
  60. case '4':
  61. throw new UploadException( '没有文件上传!',4);
  62. break;
  63. case '6':
  64. throw new UploadException( '文件上传成功找不到临时文件夹!',6);
  65. echo '<hr>';
  66. break;
  67. case '7':
  68. throw new UploadException( '文件写入失败!',7);
  69. echo '<hr>';
  70. break;
  71. default:
  72. throw new UploadException('未知类型错误', 8);
  73. }
  74. // 将文件从临时目录 移动到用户自定义的目标目录中
  75. // 临时文件名
  76. $tempFilename = $_FILES['my_pic']['tmp_name'];
  77. // echo $tempFilename;
  78. // die();
  79. if(is_uploaded_file($tempFilename)){
  80. // 原始文件名
  81. $originalFileName = $_FILES['my_pic']['name'];
  82. echo $originalFileName;
  83. // die();
  84. // 目录文件名
  85. $destFileName = 'uploads/'.$originalFileName;
  86. // echo $destFileName;
  87. // die();
  88. // 移动文件到目标目录使用的函数
  89. if (move_uploaded_file($tempFilename, $destFileName)) {
  90. echo "<p>$originalFileName: 上传成功~~</p>";
  91. // 预览
  92. echo "<img src='{$destFileName}' width='200'>";
  93. }
  94. }
  95. }catch (UploadException $e) {
  96. echo $e;
  97. }
  98. ?>
  99. <!DOCTYPE html>
  100. <html lang="en">
  101. <head>
  102. <meta charset="UTF-8">
  103. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  104. <title>单个文件上传</title>
  105. </head>
  106. <body>
  107. <form action="" method="post" enctype="multipart/form-data" >
  108. <fieldset>
  109. <legend>单个文件上传</legend>
  110. <input type="hidden" name="MAX_FILE_SIZE" value="3000000" >
  111. <input type="file" name="my_pic" id="my_pic">
  112. <button type="submit">上传</button>
  113. </fieldset>
  114. </form>
  115. </body>
  116. </html>

上传成功的演示效果

上传出错的演示效果

多文件上传(一):逐个上传

  1. <?php
  2. // echo '超全局变量 $_FILES 数组的键值:';
  3. printf('<pre>%s</pre>',print_r($_FILES, true));
  4. echo '<hr>';
  5. // 自定义上传异常类
  6. class UploadException extends Exception
  7. {
  8. // 在异常子类中,可以访问并重写Exception中的四个属性,通过__toString()格式化异常输出信息
  9. public function __toString()
  10. {
  11. return <<< UPLOAD
  12. <style>
  13. table {border-collapse: collapse;border:1px solid black;text-align: center;}
  14. td {border:1px solid black;padding: 5px;}
  15. tr:first-of-type {background-color:#eee;}
  16. tr:last-of-type td {color: coral;}
  17. </style>
  18. <table>
  19. <tr><td>代码</td><td>信息</td><td>文件</td><td>行号</td></tr>
  20. <tr><td>$this->code</td><td>$this->message</td><td>$this->file</td><td>$this->line</td></tr>
  21. </table>
  22. UPLOAD;
  23. }
  24. }
  25. try{
  26. // 判断文件出错信息
  27. echo '三个文件上传状态 : ';
  28. foreach($_FILES as $file){
  29. echo $file['error'];
  30. // die();
  31. }
  32. // $errcode = $_FILES['my_pic']['error'];
  33. switch($file['error']){
  34. case '0':
  35. echo '文件上传成功!';
  36. echo '<hr>';
  37. break;
  38. case '1':
  39. throw new UploadException('文件超过`php.ini`中`upload_max_filesize`值!',1);
  40. echo '<hr>';
  41. break;
  42. case '2':
  43. throw new UploadException( '文件大小超过表单中`MAX_FILE_SIZE`指定的值!',2);
  44. echo '<hr>';
  45. break;
  46. case '3':
  47. throw new UploadException( '文件只有部分被上传!',3);
  48. echo '<hr>';
  49. break;
  50. case '4':
  51. throw new UploadException( '没有文件上传!',4);
  52. break;
  53. case '6':
  54. throw new UploadException( '文件上传成功找不到临时文件夹!',6);
  55. echo '<hr>';
  56. break;
  57. case '7':
  58. throw new UploadException( '文件写入失败!',7);
  59. echo '<hr>';
  60. break;
  61. default:
  62. throw new UploadException('未知类型错误', 8);
  63. }
  64. foreach($_FILES as $file){
  65. // 只要判断 error === 0
  66. if ($file['error'] === 0){
  67. $destFileName = 'uploads/'.$file['name'];
  68. echo $destFileName;
  69. move_uploaded_file($file['tmp_name'] , $destFileName);
  70. echo "<img src='{$destFileName}' width='200' >" ;
  71. }
  72. }
  73. }catch (UploadException $e) {
  74. echo $e;
  75. }
  76. ?>
  77. <!DOCTYPE html>
  78. <html lang="en">
  79. <head>
  80. <meta charset="UTF-8">
  81. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  82. <title>多个文件上传</title>
  83. </head>
  84. <body>
  85. <form action="" method="post" enctype="multipart/form-data" >
  86. <fieldset>
  87. <legend>多个文件上传:逐个上传</legend>
  88. <input type="hidden" name="MAX_FILE_SIZE" value="3000000" >
  89. <input type="file" name="my_pic1" id="my_pic1">
  90. <input type="file" name="my_pic2" id="my_pic2">
  91. <input type="file" name="my_pic3" id="my_pic3">
  92. <button type="submit">上传</button>
  93. </fieldset>
  94. </form>
  95. </body>
  96. </html>

上传成功的演示效果

上传出错的演示效果

多个文件上传(二)批量上传

  1. <?php
  2. // echo '超全局变量 $_FILES 数组的键值:';
  3. printf('<pre>%s</pre>',print_r($_FILES, true));
  4. echo '<hr>';
  5. // 自定义上传异常类
  6. class UploadException extends Exception
  7. {
  8. // 在异常子类中,可以访问并重写Exception中的四个属性,通过__toString()格式化异常输出信息
  9. public function __toString()
  10. {
  11. return <<< UPLOAD
  12. <style>
  13. table {border-collapse: collapse;border:1px solid black;text-align: center;}
  14. td {border:1px solid black;padding: 5px;}
  15. tr:first-of-type {background-color:#eee;}
  16. tr:last-of-type td {color: coral;}
  17. </style>
  18. <table>
  19. <tr><td>代码</td><td>信息</td><td>文件</td><td>行号</td></tr>
  20. <tr><td>$this->code</td><td>$this->message</td><td>$this->file</td><td>$this->line</td></tr>
  21. </table>
  22. UPLOAD;
  23. }
  24. }
  25. try{
  26. // 判断文件出错信息
  27. echo '多个文件上传状态 : ';
  28. foreach($_FILES['my_pic']['error'] as $key=>$error){
  29. echo $error;
  30. // die();
  31. }
  32. // $errcode = $_FILES['my_pic']['error'];
  33. switch( $error){
  34. case '0':
  35. echo '文件上传成功!';
  36. echo '<hr>';
  37. break;
  38. case '1':
  39. throw new UploadException('文件超过`php.ini`中`upload_max_filesize`值!',1);
  40. echo '<hr>';
  41. break;
  42. case '2':
  43. throw new UploadException( '文件大小超过表单中`MAX_FILE_SIZE`指定的值!',2);
  44. echo '<hr>';
  45. break;
  46. case '3':
  47. throw new UploadException( '文件只有部分被上传!',3);
  48. echo '<hr>';
  49. break;
  50. case '4':
  51. throw new UploadException( '没有文件上传!',4);
  52. break;
  53. case '6':
  54. throw new UploadException( '文件上传成功找不到临时文件夹!',6);
  55. echo '<hr>';
  56. break;
  57. case '7':
  58. throw new UploadException( '文件写入失败!',7);
  59. echo '<hr>';
  60. break;
  61. default:
  62. throw new UploadException('未知类型错误', 8);
  63. }
  64. if($_FILES['my_pic'])
  65. foreach($_FILES['my_pic']['error'] as $key=>$error){
  66. // 只要判断 error === 0
  67. if ($error === 0){
  68. $tempFileName = $_FILES['my_pic']['tmp_name'][$key];
  69. $origFileName = $_FILES['my_pic']['name'][$key];
  70. $destFileName = 'uploads/'.$origFileName;
  71. move_uploaded_file($tempFileName , $destFileName);
  72. echo "<img src='{$destFileName}' width='200' >" ;
  73. }
  74. }
  75. }catch (UploadException $e) {
  76. echo $e;
  77. }
  78. ?>
  79. <!DOCTYPE html>
  80. <html lang="en">
  81. <head>
  82. <meta charset="UTF-8">
  83. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  84. <title>多个文件上传</title>
  85. </head>
  86. <body>
  87. <form action="" method="post" enctype="multipart/form-data" >
  88. <fieldset>
  89. <legend>多个文件上传:批量上传</legend>
  90. <input type="hidden" name="MAX_FILE_SIZE" value="3000000" >
  91. <input type="file" name="my_pic[]" id="my_pic" multiple >
  92. <button type="submit">上传</button>
  93. </fieldset>
  94. </form>
  95. </body>
  96. </html>

上传成功的演示效果

上传出错的效果

总结:
1.文件上传有七个配置项,要熟记。
2.单个文件上传::熟记一个超全局变量:$_FILES
——它是一个二维数组,共有 5 个键名:

序号 键名 描述
1 name 文件在客户端的原始文件名(即存在用户电脑上的文件名)
2 type 文件的 MIME 类型, 由浏览器提供, PHP 并不检查它
3 tmp_name 文件被上传到服务器上之后,在临时目录中临时文件名
4 error 和该文件上传相关的错误代码
5 size 已上传文件的大小(单位为字节)

3.多文件上传
—逐个上传的异常处理有所不同,用了foreach 处理error还是不完美,只能显示三个上传都出错的的画面。
—批量上传:可能代码多,都不能上传效果图了。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:上传, 想做到完美, 是非常困难的, 但做对不难
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post