Blogger Information
Blog 37
fans 0
comment 0
visits 14254
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
upload文件上传
秋闲独醉
Original
390 people have browsed it

单文件上传

  1. <?php
  2. if(isset($_FILES['userfile'])){
  3. $error = $_FILES['userfile']['error'];
  4. // var_dump($error);
  5. if($error){
  6. switch($error){
  7. case 1: echo "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值.";
  8. break;
  9. case 2: echo "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值.";
  10. break;
  11. case 3: echo "文件只有部分被上传。";
  12. break;
  13. case 4: echo "没有文件被上传。";
  14. break;
  15. case 6: echo "找不到临时文件夹。";
  16. break;
  17. case 7: echo "文件写入失败。";
  18. default: echo "未知错误";
  19. }
  20. }else{
  21. $name =pathinfo($_FILES['userfile']['name'])['filename'];
  22. $ext = pathinfo($_FILES['userfile']['name'])['extension'];
  23. $newFileName = 'upload/'.md5($name).'.'.$ext;
  24. // var_dump($newFileName,$_FILES['userfile']['tmp_name']);
  25. if(move_uploaded_file($_FILES['userfile']['tmp_name'],"$newFileName")){
  26. echo "上传成功";
  27. }else{
  28. echo '123';
  29. }
  30. }
  31. }
  32. ?>
  33. <!DOCTYPE html>
  34. <html lang="en">
  35. <head>
  36. <meta charset="UTF-8">
  37. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  38. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  39. <title>文件上传功能</title>
  40. </head>
  41. <body>
  42. <h1>单文件上传功能</h1>
  43. <form action="demo.php" method="POST" enctype="multipart/form-data">
  44. <input type="hidden" name="MAX_FILE_SIZE" value="1048576">
  45. <input type="file" name="userfile" id="">
  46. <button>上传</button>
  47. </form>
  48. </body>
  49. </html>

多文件上传

  1. <?php
  2. // var_dump($_FILES);
  3. $message = "";
  4. foreach($_FILES['userfile']['error'] as $key => $value){
  5. if(!$value){
  6. switch($value){
  7. case 1: $message .="第{$key}个文件上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。"."<br>";
  8. break;
  9. case 2: $message .="第{$key}个文件上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。"."<br>";
  10. break;
  11. case 3: $message .="第{$key}个文件文件只有部分被上传。"."<br>";
  12. break;
  13. case 4: $message .="第{$key}个文件没有文件被上传。"."<br>";
  14. break;
  15. case 6: $message .="第{$key}个文件找不到临时文件夹。"."<br>";
  16. break;
  17. case 7: $message .="第{$key}个文件文件写入失败。"."<br>";
  18. // default: echo "未知错误";
  19. }
  20. }
  21. }
  22. if($message){
  23. echo $message;
  24. exit;
  25. }
  26. foreach($_FILES['userfile']['error'] as $key => $value){
  27. if(is_uploaded_file($_FILES['userfile']['tmp_name'][$key])){
  28. $newFileName = 'upload/'.$_FILES['userfile']['name'][$key];
  29. $oldFileName = $_FILES['userfile']['tmp_name'][$key];
  30. move_uploaded_file($oldFileName,$newFileName);
  31. echo "上传成功";
  32. }else{
  33. echo "未知错误";
  34. }
  35. }
  36. ?>
  37. <!DOCTYPE html>
  38. <html lang="en">
  39. <head>
  40. <meta charset="UTF-8">
  41. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  42. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  43. <title>文件上传功能</title>
  44. </head>
  45. <body>
  46. <h1>多文件上传功能</h1>
  47. <form action="demo.php" method="POST" enctype="multipart/form-data">
  48. <!-- <input type="hidden" name="MAX_FILE_SIZE" value="1048576"> -->
  49. <input type="file" name="userfile[]" id="" multiple>
  50. <button>上传</button>
  51. </form>
  52. </body>
  53. </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