Blogger Information
Blog 21
fans 0
comment 0
visits 10917
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
单文件上传
Original
713 people have browsed it

单文件上传php代码

  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. * 6.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 .= '上传文件的大小超过了 HTML 表单中 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. if (is_uploaded_file($tmpName)) {
  43. $allow = ['jpg','jpeg','png','gif'];
  44. $ext = pathinfo($name)['extension'];
  45. if (in_array($ext, $allow)) {
  46. $path = 'uploads/';
  47. $dest = $path . md5($name) . '.' . $ext;
  48. if (move_uploaded_file($tmpName, $dest)) {
  49. echo '上传成功';
  50. echo "<img src='$dest', width='200'>";
  51. } else {
  52. echo '移动失败';
  53. }
  54. } else {
  55. echo '文件类型错误';
  56. }
  57. } else {
  58. echo '非法方式上传';
  59. }
  60. }
  61. }
  62. ?>
  63. <head>
  64. <meta charset="UTF-8">
  65. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  66. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  67. <title>Document</title>
  68. </head>
  69. <body>
  70. <form action="" method="POST" enctype="multipart/form-data">
  71. <fieldset>
  72. <legend>单文件上传</legend>
  73. <input type="hidden" name="MAX_FILE_SIZE" value="400000">
  74. <input type="file" name="my_pic">
  75. <button>上传</button>
  76. </fieldset>
  77. </form>
  78. </body>
  79. </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