Blogger Information
Blog 49
fans 0
comment 3
visits 23007
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示单文件与多文件上传及其错误处理
P粉479712293
Original
624 people have browsed it

题目1:单文件上传及其错误处理

1.demo1.php文件:

  1. <?php
  2. // *单文件上传
  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">上传失败!</span><br>';
  13. switch ($error) {
  14. case 1:
  15. $tips .= '大小超过了php.ini中的允许上传的文件大小';
  16. break;
  17. case 2:
  18. $tips .= '大小超过了表单中(MAX_FILE_SIZE)的允许上传的文件大小';
  19. break;
  20. case 3:
  21. $tips .= '文件只有部分被上传';
  22. break;
  23. case 4:
  24. $tips .= '没有文件被上传';
  25. break;
  26. case 6:
  27. $tips .= '找不到临时目录';
  28. break;
  29. case 7:
  30. $tips .= '文件写入失败,请检查目录权限';
  31. break;
  32. }
  33. echo "<p>$tips</p>";
  34. } else {
  35. //*1 判断上传方式是否合法? POST
  36. if (is_uploaded_file($tmpName)) {
  37. //* 文件类型白名单
  38. $allow = ['jpg','jpeg','png','gif','JPG','JPEG','PNG','GIF'];
  39. // *采用pathinfo('xxx.xx')就可得到其后缀名'.xx'
  40. //*后缀名$ext
  41. $ext = pathinfo($name)['extension'];
  42. // *in_array(x,y):判断某一个值x,是否在数组y中
  43. if (in_array($ext, $allow)) {
  44. // *如果上传方式合法,且是允许的类型,则可以移动到指定的目录中了
  45. $path = 'uploads/';
  46. // *为了防止同名覆盖,应该给目标文件名重命名
  47. // *其中md5(x)是对其加密
  48. $dest =$path. md5($name) . '.' . $ext;
  49. // *将文件从临时目录移动到目标目录中
  50. if (move_uploaded_file($tmpName, $dest)) {
  51. echo '<p>上传成功</p>';
  52. //* 预览
  53. echo "<img src='$dest' width='300'>";
  54. } else {
  55. echo '<p>上传不成功</p>';
  56. }
  57. } else {
  58. echo '<p>文件类型错误</p>';
  59. }
  60. } else {
  61. echo '<p>上传方式非法</p>';
  62. }
  63. // echo '上传成功了,进行下一步操作';
  64. }
  65. }
  66. ?>
  67. <!DOCTYPE html>
  68. <html lang="zh-CN">
  69. <head>
  70. <meta charset="UTF-8">
  71. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  72. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  73. <title>单文件上传</title>
  74. </head>
  75. <body>
  76. <form action="" method="POST" enctype="multipart/form-data">
  77. <fieldset>
  78. <legend>单文件上传</legend>
  79. <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
  80. <input type="file" name="my_pic">
  81. <button>上传</button>
  82. </fieldset>
  83. </form>
  84. </body>
  85. </html>

2.浏览器效果图:

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

1.demo2.php文件:

  1. <?php
  2. // *多文件上传-逐个上传
  3. printf('<pre>%s</pre>', print_r($_FILES, true));
  4. foreach ($_FILES as $file) {
  5. //* 只要判断$file['error'] === 0 表示上传成功
  6. if ($file['error'] === 0) {
  7. // *暂不加密目标文件名
  8. $dest = 'uploads/' . $file['name'];
  9. // *移动到目标目录中
  10. move_uploaded_file($file['tmp_name'], $dest) ;
  11. // *预览
  12. echo "<img src='$dest' width='200'>";
  13. }
  14. }
  15. ?>
  16. <!DOCTYPE html>
  17. <html lang="zh-CN">
  18. <head>
  19. <meta charset="UTF-8">
  20. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  21. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  22. <title>多文件上传-1</title>
  23. </head>
  24. <body>
  25. <form action="" method="POST" enctype="multipart/form-data">
  26. <fieldset>
  27. <legend>多文件上传-1:逐个上传</legend>
  28. <input type="file" name="my_pic1">
  29. <input type="file" name="my_pic2">
  30. <input type="file" name="my_pic3">
  31. <button>上传</button>
  32. </fieldset>
  33. </form>
  34. </body>
  35. </html>

2.浏览器效果图:

题目3:多文件上传-逐个上传采用二维数组形式

1.demo3.php文件:

  1. <?php
  2. // *多文件上传-逐个上传采用二维数组形式
  3. //* $_FILES
  4. printf('<pre>%s</pre>', print_r($_FILES, true));
  5. // *判断服务器中超全局变量$_FILES中的my_pic数组是否存在
  6. if (isset($_FILES['my_pic'])) {
  7. // *遍历二维数组中的[error]键
  8. foreach ($_FILES['my_pic']['error'] as $key=>$error) {
  9. //* 0 => UPLOAD_ERR_OK
  10. // *如果error键的值为0
  11. if ($error === UPLOAD_ERR_OK) {
  12. // *临时文件名$tmpName
  13. // *$key:键变量,分别代表[0],[1],[2]
  14. $tmpName = $_FILES['my_pic']['tmp_name'][$key];
  15. //* 原始文件名
  16. $name = $_FILES['my_pic']['name'][$key];
  17. // *暂不加密目标文件名
  18. $dest = 'uploads/' . $name;
  19. // *移动到目标目录中
  20. move_uploaded_file($tmpName, $dest) ;
  21. // *预览
  22. echo "<img src='$dest' width='200'>";
  23. }
  24. }
  25. }
  26. ?>
  27. <!DOCTYPE html>
  28. <html lang="zh-CN">
  29. <head>
  30. <meta charset="UTF-8">
  31. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  32. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  33. <title>多文件上传-2</title>
  34. </head>
  35. <body>
  36. <form action="" method="POST" enctype="multipart/form-data">
  37. <fieldset>
  38. <legend>多文件上传-2:逐个上传</legend>
  39. <input type="file" name="my_pic[]">
  40. <input type="file" name="my_pic[]">
  41. <input type="file" name="my_pic[]">
  42. <button>上传</button>
  43. </fieldset>
  44. </form>
  45. </body>
  46. </html>

2.浏览器效果图:

题目4:多文件上传-批量上传

1.demo4.php文件:

  1. <?php
  2. // *多文件上传-批量上传
  3. //* $_FILES
  4. printf('<pre>%s</pre>', print_r($_FILES, true));
  5. // *判断服务器中超全局变量$_FILES中的my_pic数组是否存在
  6. if (isset($_FILES['my_pic'])) {
  7. // *遍历二维数组中的[error]键
  8. foreach ($_FILES['my_pic']['error'] as $key=>$error) {
  9. //* 0 => UPLOAD_ERR_OK
  10. // *如果error键的值为0
  11. if ($error === UPLOAD_ERR_OK) {
  12. // *临时文件名$tmpName
  13. // *$key:键变量,分别代表[0],[1],[2]
  14. $tmpName = $_FILES['my_pic']['tmp_name'][$key];
  15. //* 原始文件名
  16. $name = $_FILES['my_pic']['name'][$key];
  17. // *暂不加密目标文件名
  18. $dest = 'uploads/' . $name;
  19. // *移动到目标目录中
  20. move_uploaded_file($tmpName, $dest) ;
  21. // *预览
  22. echo "<img src='$dest' width='200'>";
  23. }
  24. }
  25. }
  26. ?>
  27. <!DOCTYPE html>
  28. <html lang="zh-CN">
  29. <head>
  30. <meta charset="UTF-8">
  31. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  32. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  33. <title>多文件上传-2</title>
  34. </head>
  35. <body>
  36. <form action="" method="POST" enctype="multipart/form-data">
  37. <fieldset>
  38. <legend>多文件上传-3:批量上传</legend>
  39. <!-- multiple: 允许同时选择多个 -->
  40. <input type="file" name="my_pic[]" multiple>
  41. <button>上传</button>
  42. <!-- 与demo3.php相比,只改了二个地方 -->
  43. <!-- 1. 只保留一个my_pic[] -->
  44. <!-- 2. 加一个 multiple 属性,支持多选 -->
  45. <!-- php处理上传的代码和demo3.php完全一样,不用动 -->
  46. </fieldset>
  47. </form>
  48. </body>
  49. </html>

2.浏览器效果图:

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