Blogger Information
Blog 34
fans 0
comment 0
visits 20016
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
单文件和多文件上传
OC的PHP大牛之路
Original
455 people have browsed it

文件上传

  1. <?php
  2. // $_FILES
  3. printf('<pre>%s</pre>', print_r($_FILES, true));
  4. if (isset($_FILES['my_pic'])) {
  5. // 原始文件名
  6. $name = $_FILES['my_pic']['name'];
  7. // 临时文件名
  8. $tmpName = $_FILES['my_pic']['tmp_name'];
  9. // 错误代码
  10. $error = $_FILES['my_pic']['error'];
  11. if ($error >0) {
  12. $tips = '<span style="color:red">上传失败!</
  13. span><br>';
  14. switch ($error) {
  15. case 1:
  16. $tips .= '大小超过了php.ini中的允许上传的
  17. 文件大小';
  18. break;
  19. case 2:
  20. $tips .= '大小超过了表单中(MAX_FILE_SIZE)
  21. 的允许上传的文件大小';
  22. break;
  23. case 3:
  24. $tips .= '文件只有部分被上传';
  25. break;
  26. case 4:
  27. $tips .= '没有文件被上传';
  28. break;
  29. case 6:
  30. $tips .= '找不到临时目录';
  31. break;
  32. case 7:
  33. $tips .= '文件写入失败,请检查目录权限';
  34. break;
  35. }
  36. echo "<p>$tips</p>";
  37. } else {
  38. //1 判断上传方式是否合法? POST
  39. if (is_uploaded_file($tmpName)) {
  40. // 文件类型白名单
  41. $allow = ['jpg','jpeg','png','gif'];
  42. // 扩展名
  43. $ext = pathinfo($name)['extension'];
  44. if (in_array($ext, $allow)) {
  45. // 如果上传方式合法,且是允许的类型,则可以移
  46. 动到指定的目录中了
  47. $path = 'uploads/';
  48. // 为了防止同名覆盖,应该给目标文件名重命名
  49. $dest =$path. md5($name) . '.' . $ext;
  50. // 将文件从临时目录移动到目标目录中
  51. if (move_uploaded_file($tmpName,
  52. $dest)) {
  53. echo '<p>上传成功</p>';
  54. // 预览
  55. echo "<img src='$dest'
  56. width='300'>";
  57. } else {
  58. }
  59. } else {
  60. echo '<p>文件类型错误</p>';
  61. }
  62. } else {
  63. echo '<p>上传方式非法</p>';
  64. }
  65. }
  66. }
  67. ?>

单文件上传

  1. <body>
  2. <form action="" method="POST" enctype="multipart/
  3. form-data">
  4. <fieldset>
  5. <legend>单文件上传</legend>
  6. <input type="file" name="my_pic">
  7. <button>上传</button>
  8. </fieldset>
  9. </form>
  10. </body>

多文件上传-1:逐个上传

  1. <body>
  2. <form action="" method="POST" enctype="multipart/
  3. form-data">
  4. <fieldset>
  5. <legend>多文件上传-1:逐个上传</legend>
  6. <input type="file" name="my_pic1">
  7. <input type="file" name="my_pic2">
  8. <input type="file" name="my_pic3">
  9. <button>上传</button>
  10. </fieldset>
  11. </form>
  12. </body>

多文件上传-2:逐个上传

  1. <body>
  2. <form action="" method="POST" enctype="multipart/
  3. form-data">
  4. <fieldset>
  5. <legend>多文件上传-2:逐个上传</legend>
  6. <input type="file" name="my_pic[]">
  7. <input type="file" name="my_pic[]">
  8. <input type="file" name="my_pic[]">
  9. <button>上传</button>
  10. </fieldset>
  11. </form>
  12. </body>

多文件上传-3:批量上传

  1. <body>
  2. <form action="" method="POST" enctype="multipart/
  3. form-data">
  4. <fieldset>
  5. <legend>多文件上传-3:批量上传</legend>
  6. <!-- multiple: 允许同时选择多个 -->
  7. <input type="file" name="my_pic[]" multiple>
  8. <button>上传</button>
  9. </fieldset>
  10. </form>
  11. </body>
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