Blogger Information
Blog 43
fans 4
comment 0
visits 18995
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP之文件上传/单文件/多文件
汇享科技
Original
741 people have browsed it

1. 单文件上传

  • 效果图如下:
    71994-gi5zgkmewdh.png

  • 代码如下:

  1. //单文件上传
  2. // print_r($_FILES);
  3. //1.上传文件不能为空
  4. if (isset($_FILES['tupian'])) {
  5. // 获取到当前的图片原始名
  6. $name = $_FILES['tupian']['name'];
  7. // 获取到临时文件名
  8. $tmpName = $_FILES['tupian']['tmp_name'];
  9. // 获取上传错误
  10. $error = $_FILES['tupian']['error'];
  11. //判断是否上传错误
  12. //当error大于0的时候就是有错误
  13. if ($error > 0) {
  14. //使用switch判断
  15. $cw = '<p style="color:red">上传失败错误原因:</p>';
  16. switch ($error) {
  17. //当错误为1时....
  18. case 1:
  19. $cw .= '文件过大超过配置文件限制大小';
  20. break;
  21. case 2:
  22. $cw .= '要上传的文件大小超出浏览器限制';
  23. break;
  24. case 3:
  25. $cw .= '文件仅部分被上传';
  26. break;
  27. case 4:
  28. $cw .= '没有找到要上传的文件';
  29. break;
  30. case 5:
  31. $cw .= '服务器临时文件夹丢失';
  32. break;
  33. case 6:
  34. $cw .= '文件写入到临时文件夹出错';
  35. break;
  36. case 7:
  37. $cw .= '文件写入失败,请检查目录权限';
  38. break;
  39. }
  40. echo $cw;
  41. } else {
  42. //没有错误继续进行操作
  43. //当没有错误的时候 先进行判断文件上传是否合法POST
  44. // is_uploaded_file:判断文件是否是通过 POST 上传的
  45. if (is_uploaded_file($tmpName)) {
  46. //如果是继续判断文件类型是否正确
  47. $hz = ['jpg', 'png', 'gif', 'jpeg', 'webp'];
  48. //拿到文件名后缀
  49. $houzhui = pathinfo($name)['extension'];
  50. //in_array用来查询数组中是否存在某个元素
  51. if (in_array($houzhui, $hz)) {
  52. // 如果合法那么继续上传
  53. //创建目录名称等会将临时文件移动到指定目录
  54. $path = 'images/';
  55. //防止重复名 使用md5加密原文件名+时间戳
  56. $mz = $path . md5($name) . time() . '.' . $houzhui;
  57. //将临时文件上传到指定目录
  58. // move_uploaded_file:将上传的文件移动到新位置
  59. if (move_uploaded_file($tmpName, $mz)) {
  60. //上传成功之后打印即可
  61. echo '上传成功';
  62. echo "<img src='$mz' width='100'/>";
  63. }
  64. } else {
  65. echo '文件类型不存在';
  66. }
  67. } else {
  68. echo '非法上传';
  69. }
  70. }
  71. }
  72. ?>
  1. <body>
  2. <form action="" method="post" enctype="multipart/form-data">
  3. <fieldset>
  4. <legend>单文件上传</legend>
  5. <input type="file" name="tupian">
  6. <button>上传</button>
  7. </fieldset>
  8. </form>
  9. </body>

2. 多文件上传-1

  • 效果图如下:
    45104-9i7q8j64ie6.png

  • 代码如下:

  1. <?php
  2. //多文件上传
  3. // printf('<pre>%s</pre>', print_r($_FILES, true));
  4. //使用foreach遍历
  5. foreach($_FILES as $file){
  6. //判断是否为有错误 如果等于0就代表上传成功开始处理
  7. if($file['error'] === 0){
  8. if (is_uploaded_file($file['tmp_name'])) {
  9. //如果是继续判断文件类型是否正确
  10. $hz = ['jpg', 'png', 'gif', 'jpeg', 'webp'];
  11. //拿到文件名后缀
  12. $houzhui = pathinfo($file['name'])['extension'];
  13. //in_array用来查询数组中是否存在某个元素
  14. if (in_array($houzhui, $hz)) {
  15. // 如果合法那么继续上传
  16. //创建目录名称等会将临时文件移动到指定目录
  17. $path = 'images/';
  18. //防止重复名 使用md5加密原文件名+时间戳
  19. $mz = $path . md5($file['name']) . time() . '.' . $houzhui;
  20. //将临时文件上传到指定目录
  21. // move_uploaded_file:将上传的文件移动到新位置
  22. if (move_uploaded_file($file['tmp_name'], $mz)) {
  23. //上传成功之后打印即可
  24. echo '上传成功';
  25. echo "<img src='$mz' width='100'/>";
  26. }
  27. } else {
  28. echo '文件类型不存在';
  29. }
  30. } else {
  31. echo '非法上传';
  32. }
  33. //将文件移入到指定目录
  34. }
  35. }
  36. ?>
  1. <body>
  2. <form action="" method="post" enctype="multipart/form-data">
  3. <fieldset>
  4. <legend>多文件上传1-逐条上传</legend>
  5. <input type="file" name="tupian">
  6. <input type="file" name="tupian1">
  7. <input type="file" name="tupian2">
  8. <button>上传</button>
  9. </fieldset>
  10. </form>
  11. </body>

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

  • 效果图如下:
    48413-b5gzjldpt4.png

  • 代码如下:

  1. <?php
  2. //多文件上传
  3. // printf('<pre>%s</pre>', print_r($_FILES, true));
  4. //判断是否为空
  5. if (isset($_FILES['tupian'])) {
  6. // 使用循环遍历
  7. foreach($_FILES['tupian']['error'] as $k=>$error){
  8. if($error === UPLOAD_ERR_OK){
  9. $name = $_FILES['tupian']['name'][$k];
  10. $tmpName = $_FILES['tupian']['tmp_name'][$k];
  11. if (is_uploaded_file($tmpName)) {
  12. $hz = ['jpg', 'png', 'gif', 'jpeg', 'webp'];
  13. //拿到文件名后缀
  14. $houzhui = pathinfo($name)['extension'];
  15. //in_array用来查询数组中是否存在某个元素
  16. if (in_array($houzhui, $hz)) {
  17. // 如果合法那么继续上传
  18. //创建目录名称等会将临时文件移动到指定目录
  19. $path = 'images/';
  20. //防止重复名 使用md5加密原文件名+时间戳
  21. $mz = $path . md5($name) . time() . '.' . $houzhui;
  22. //将临时文件上传到指定目录
  23. // move_uploaded_file:将上传的文件移动到新位置
  24. if (move_uploaded_file($tmpName, $mz)) {
  25. //上传成功之后打印即可
  26. echo '上传成功';
  27. echo "<img src='$mz' width='100'/>";
  28. }
  29. } else {
  30. echo '文件类型不存在';
  31. }
  32. } else {
  33. echo '非法上传';
  34. }
  35. }
  36. }
  37. }
  38. ?>
  1. <body>
  2. <form action="" method="post" enctype="multipart/form-data">
  3. <fieldset>
  4. <legend>多文件上传-批量上传</legend>
  5. <input type="file" name="tupian[]" multiple>
  6. <button>上传</button>
  7. </fieldset>
  8. </form>
  9. </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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!