Blogger Information
Blog 35
fans 0
comment 0
visits 17136
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0818-文件上传与错误处理
三九三伏
Original
395 people have browsed it

文件上传与错误处理

上传文件设置两个属性

  1. method:POST,GET在地址栏,最大数据量才4K,不满足大文件需求。
  2. enctype=”multipart/form-data”

单文件上传

  1. <?php
  2. printf('<pre>%s</pre>',print_r($_FILES,true));
  3. if(isset($_FILES['my_pic'])){
  4. //原始文件名
  5. $name = $_FILES['my_pic']['name'];
  6. // 临时文件名
  7. $tmpName = $_FILES['my_pic']['tmp_name'];
  8. // 错误代码
  9. $error = $_FILES['my_pic']['error'];
  10. if($error > 0){
  11. $tips = '<span style="color:red">上传失败!</span><br>';
  12. switch($error){
  13. case 1:
  14. $tips .= '超过php.ini允许上传的大小!';
  15. break;
  16. case 2:
  17. $tips .= '超过表单中MAX_FILE_SIZE允许上传的大小!';
  18. break;
  19. case 3:
  20. $tips .= '文件只有部分被上传!';
  21. break;
  22. case 4:
  23. $tips .= '没有文件被上传!';
  24. break;
  25. case 6:
  26. $tips .= '找不到临时目录!';
  27. break;
  28. case 7:
  29. $tips .= '文件写入失败,请检查目录权限!';
  30. break;
  31. }
  32. echo "<p>$tips</p>";
  33. }else{
  34. // echo '上传成功!';
  35. // 判断上传方式是否合法?
  36. if(is_uploaded_file($tmpName)){
  37. // 文件类型白名单
  38. $allow = ['jpg', 'jpeg', 'png', 'gif'];
  39. // 获取扩展名
  40. $ext = pathinfo($name)['extension'];
  41. if(in_array($ext, $allow)){
  42. // 类型合法,设置指定目录
  43. $path = 'uploads/';
  44. // 重命名,防止重名。
  45. $dest = $path.md5($tmpName).'.'.$ext;
  46. // 移动到指定目录
  47. if(move_uploaded_file($tmpName, $dest)){
  48. echo '上传成功!';
  49. echo "<img src='$dest' width='50'>";
  50. }else{
  51. echo '上传失败!';
  52. }
  53. }else{
  54. echo '<p>不允许的类型!</p>';
  55. }
  56. }else{
  57. echo '<p>非法方式上传!</p>';
  58. }
  59. }
  60. }
  61. ?>
  62. <!DOCTYPE html>
  63. <html lang="en">
  64. <head>
  65. <meta charset="UTF-8">
  66. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  67. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  68. <title>Document</title>
  69. </head>
  70. <body>
  71. <form action="" method="POST" enctype="multipart/form-data">
  72. <fieldset>
  73. <legend>单文件上传</legend>
  74. <!-- name="my_pic":给服务器段语言用的变量,PHP可以用$FILE来获取。 -->
  75. <!-- <input type="hidden" name="MAX_FILE_SIZE" value="8"> -->
  76. <input type="file" name="my_pic">
  77. <button>上传</button>
  78. </fieldset>
  79. </form>
  80. </body>
  81. </html>

多文件上传

逐个上传

方式1

  1. <?php
  2. printf('<pre>%s</pre>',print_r($_FILES,true));
  3. foreach($_FILES as $file){
  4. if($file['error'] === 0){
  5. $dest = 'uploads/'. $file['name'];
  6. move_uploaded_file($file['tmp_name'], $dest);
  7. echo "<img src='$dest' width='150'>";
  8. }
  9. }
  10. ?>
  11. <!DOCTYPE html>
  12. <html lang="en">
  13. <head>
  14. <meta charset="UTF-8">
  15. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  16. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  17. <title>Document</title>
  18. </head>
  19. <body>
  20. <form action="" method="POST" enctype="multipart/form-data">
  21. <fieldset>
  22. <legend>多文件上传-1:逐个上传</legend>
  23. <input type="file" name="my_pic1">
  24. <input type="file" name="my_pic2">
  25. <input type="file" name="my_pic3">
  26. <button>上传</button>
  27. </fieldset>
  28. </form>
  29. </body>
  30. </html>

方式2

  1. <?php
  2. printf('<pre>%s</pre>',print_r($_FILES,true));
  3. if(isset($_FILES['my_pic'])){
  4. foreach($_FILES['my_pic']['error'] as $key=>$error){
  5. // php5.3以后支持UPLOAD_ERR_OK常量
  6. if($error === UPLOAD_ERR_OK){
  7. // 临时文件名
  8. $tmpName = $_FILES['my_pic']['tmp_name'][$key];
  9. // 原始文件名
  10. $name = $_FILES['my_pic']['name'][$key];
  11. $dest = 'uploads/'. $name;
  12. move_uploaded_file($tmpName, $dest);
  13. echo "<img src='$dest' width='150'>";
  14. }
  15. }
  16. }
  17. ?>
  18. <!DOCTYPE html>
  19. <html lang="en">
  20. <head>
  21. <meta charset="UTF-8">
  22. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  23. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  24. <title>Document</title>
  25. </head>
  26. <body>
  27. <form action="" method="POST" enctype="multipart/form-data">
  28. <fieldset>
  29. <legend>多文件上传-2</legend>
  30. <input type="file" name="my_pic[]">
  31. <input type="file" name="my_pic[]">
  32. <input type="file" name="my_pic[]">
  33. <button>上传</button>
  34. </fieldset>
  35. </form>
  36. </body>
  37. </html>

批量上传

  1. <?php
  2. printf('<pre>%s</pre>',print_r($_FILES,true));
  3. if(isset($_FILES['my_pic'])){
  4. foreach($_FILES['my_pic']['error'] as $key=>$error){
  5. // php5.3以后支持UPLOAD_ERR_OK常量
  6. if($error === UPLOAD_ERR_OK){
  7. // 临时文件名
  8. $tmpName = $_FILES['my_pic']['tmp_name'][$key];
  9. // 原始文件名
  10. $name = $_FILES['my_pic']['name'][$key];
  11. $dest = 'uploads/'. $name;
  12. move_uploaded_file($tmpName, $dest);
  13. echo "<img src='$dest' width='150'>";
  14. }
  15. }
  16. }
  17. ?>
  18. <!DOCTYPE html>
  19. <html lang="en">
  20. <head>
  21. <meta charset="UTF-8">
  22. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  23. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  24. <title>Document</title>
  25. </head>
  26. <body>
  27. <form action="" method="POST" enctype="multipart/form-data">
  28. <fieldset>
  29. <legend>批量上传</legend>
  30. <!-- 与逐个上传方式2相比,只保留一行下列代码,最后还要增加multiple属性。 -->
  31. <input type="file" name="my_pic[]" multiple>
  32. <button>上传</button>
  33. </fieldset>
  34. </form>
  35. </body>
  36. </html>
Correcting teacher:PHPzPHPz

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