Blogger Information
Blog 29
fans 0
comment 0
visits 19529
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP中实例演示单文件上传与多文件上传
千里马遇伯乐
Original
762 people have browsed it

单文件上传

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <?php
  4. // $_FILES: PHP超全局变量数量,保存着上传文件的全部信息
  5. printf('<pre>%s</pre>',print_r($_FILES,true));
  6. /**
  7. * 1. $_FILES: 二维数组,每个元素对应一个上传的文件
  8. * 2. name: 原始文件名
  9. * 3. type: 文件类型, mime类型
  10. * 4. tmp_name: 临时目录
  11. * 5. error: 错误代码
  12. * 5. size: 文件大小(字节表示 byte)
  13. */
  14. if (isset($_FILES['my_pic'])) {
  15. $name = $_FILES['my_pic']['name'];
  16. $tmpName = $_FILES['my_pic']['tmp_name'];
  17. $error = $_FILES['my_pic']['error'];
  18. if ($error>0) {
  19. $tips = '<span style="color:red">上传失败:</span>';
  20. switch ($error) {
  21. case 1:
  22. $tips .= '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值';
  23. break;
  24. case 2:
  25. $tips .= '文件大小超过了上传表单中MAX_FILE_SIZE最大值';
  26. break;
  27. case 3:
  28. $tips .= '文件只有部分被上传';
  29. break;
  30. case 4:
  31. $tips .= '没有文件被上传';
  32. break;
  33. case 6:
  34. $tips .= '找不到临时目录';
  35. break;
  36. case 7:
  37. $tips .= '文件写入失败,请检查目录权限';
  38. break;
  39. }
  40. echo "<p>$tips</p>";
  41. } else {
  42. // 判断用户是不是通过合法的POST方式上传
  43. if (is_uploaded_file($tmpName)) {
  44. // 设置允许上传文件类型的白名单
  45. $allow = ['jpg','jpeg', 'png', 'gif'];
  46. // printf('<pre>%s</pre>',print_r(pathinfo($name),true));
  47. // 获取文件扩展名
  48. $ext = pathinfo($name)['extension'];
  49. echo $ext.'<br>';
  50. if (in_array($ext, $allow)) {
  51. // 二个条件都满足了
  52. // 1. post方式上传的 2. 文件类型是合法的
  53. // 目标目录
  54. $path = 'uploads/';
  55. // 自定义目标文件名
  56. $dest = $path . md5($name) . '.' . $ext;
  57. echo $dest;
  58. // 将文件从临时目录中移动到目标目录中并重命名
  59. if (move_uploaded_file($tmpName, $dest)) {
  60. echo '<p style="color:green">上传成功</p>';
  61. // 预览
  62. echo "<img src='$dest' width='200' >";
  63. } else {
  64. echo '<p style="color:red">移动失败</p>';
  65. }
  66. } else {
  67. echo '<p style="color:red">文件类型错误</p>';
  68. }
  69. } else {
  70. echo '<p style="color:red">非法方式上传</p>';
  71. }
  72. }
  73. }
  74. ?>
  75. <head>
  76. <meta charset="UTF-8">
  77. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  78. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  79. <title>文件上传的表单构造,超全局变量$_FILE</title>
  80. </head>
  81. <body>
  82. <!-- 允许上传文件的表单特征
  83. 1. method="POST"
  84. 2. enctype="multipart/form-data" -->
  85. <form action="" method="POST" enctype="multipart/form-data">
  86. <fieldset>
  87. <legend>单文件上传</legend>
  88. <!-- 浏览器中限制上传文件的大小,写到一个隐藏域中,并写到type=file之前 -->
  89. <input type="hidden" name="MAX_FILE_SIZE" value="300000">
  90. <input type="file" name="my_pic">
  91. <button>上传</button>
  92. </fieldset>
  93. </form>
  94. </body>
  95. </html>

效果如下

多文件上传

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

多文件上传效果

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