Blogger Information
Blog 36
fans 0
comment 0
visits 28102
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
文件上传实例
phpcn_u202398
Original
621 people have browsed it

1、单个文件上传

代码示例
  1. <?php
  2. printf('<pre>%s</pre>', print_r($_FILES, true));
  3. $fileType = $_FILES['pic']['type'];
  4. if (strstr($fileType, '/', true) !== 'image') echo '<p>文件类型错误</p>';
  5. // 临时文件名
  6. $tempFileName = $_FILES['pic']['tmp_name'];
  7. if (is_uploaded_file($tempFileName)) {
  8. // 原始文件名
  9. $originalFileName = $_FILES['pic']['name'];
  10. // 目录文件名
  11. $destFileName = 'uploads/'.strstr($originalFileName, '.');
  12. // 移动文件到目标目录使用的函数
  13. if (move_uploaded_file($tempFileName, $destFileName)) {
  14. echo "<p>$originalFileName: 上传成功~~</p>";
  15. // 预览
  16. echo "<img src='{$destFileName}' width='200'>";
  17. }
  18. }
  19. ?>
  20. <!DOCTYPE html>
  21. <html>
  22. <head>
  23. <meta charset="utf-8">
  24. <title>单文件上传</title>
  25. </head>
  26. <body>
  27. <form action="" method="POST" enctype="multipart/form-data">
  28. <fieldset>
  29. <legend>单个文件上传</legend>
  30. <input type="hidden" name="MAX_FILE_SIZE" value="300000">
  31. <input type="file" name="pic" id="pic">
  32. <button>提交</button>
  33. </fieldset>
  34. </form>
  35. </body>
  36. </html>

2、多个文件上传

代码示例
  1. <?php
  2. printf('<pre>%s</pre>', print_r($_FILES, true));
  3. if ($_FILES['pic'])
  4. foreach ($_FILES['pic']['error'] as $key => $error) {
  5. // 只要判断 error === 0
  6. if ($error === UPLOAD_ERR_OK) {
  7. // 临时文件名
  8. $tmpFileName = $_FILES['pic']['tmp_name'][$key];
  9. // 原始文件名
  10. $originalFileName = $_FILES['pic']['name'][$key];
  11. // 目标文件名
  12. $destFileName = 'uploads/'. $originalFileName;
  13. // 移动文件
  14. move_uploaded_file($tmpFileName, $destFileName);
  15. // 预览
  16. echo "<img src='{$destFileName}' width='200'>";
  17. }
  18. }
  19. ?>
  20. <!DOCTYPE html>
  21. <html>
  22. <head>
  23. <meta charset="utf-8">
  24. <title>单文件上传</title>
  25. </head>
  26. <body>
  27. <form action="" method="POST" enctype="multipart/form-data">
  28. <fieldset>
  29. <legend>单个文件上传</legend>
  30. <input type="hidden" name="MAX_FILE_SIZE" value="300000">
  31. <input type="file" name="pic[]" id="pic">
  32. <input type="file" name="pic[]" id="pic">
  33. <input type="file" name="pic[]" id="pic">
  34. <button>提交</button>
  35. </fieldset>
  36. </form>
  37. </body>
  38. </html>

3、批量上传

代码示例
  1. <?php
  2. // $_FILES
  3. printf('<pre>%s</pre>', print_r($_FILES, true));
  4. if ($_FILES['pic'])
  5. foreach ($_FILES['pic']['error'] as $key => $error) {
  6. // 只要判断 error === 0
  7. if ($error === UPLOAD_ERR_OK) {
  8. // 临时文件名
  9. $tmpFileName = $_FILES['pic']['tmp_name'][$key];
  10. // 原始文件名
  11. $originalFileName = $_FILES['pic']['name'][$key];
  12. // 目标文件名
  13. $destFileName = 'uploads/'. $originalFileName;
  14. // 移动文件
  15. move_uploaded_file($tmpFileName, $destFileName);
  16. // 预览
  17. echo "<img src='{$destFileName}' width='200'>";
  18. }
  19. }
  20. ?>
  21. <!DOCTYPE html>
  22. <html>
  23. <head>
  24. <meta charset="utf-8">
  25. <title>单文件上传</title>
  26. </head>
  27. <body>
  28. <form action="" method="POST" enctype="multipart/form-data">
  29. <fieldset>
  30. <legend>单个文件上传</legend>
  31. <input type="hidden" name="MAX_FILE_SIZE" value="300000">
  32. <input type="file" name="pic[]" id="pic" multiple>
  33. <button>提交</button>
  34. </fieldset>
  35. </form>
  36. </body>
  37. </html>

4、封装常用上传方式

4.1、封装上传方式

代码示例
  1. <?php
  2. class Upload{
  3. //单文件上传
  4. public function one(){
  5. $fileType = $_FILES['pic']['type'];
  6. if (strstr($fileType, '/', true) !== 'image') echo '<p>文件类型错误</p>';
  7. // 临时文件名
  8. $tempFileName = $_FILES['pic']['tmp_name'];
  9. if (is_uploaded_file($tempFileName)) {
  10. // 原始文件名
  11. $originalFileName = $_FILES['pic']['name'];
  12. // 目录文件名
  13. $destFileName = 'uploads/'.strstr($originalFileName, '.');
  14. // 移动文件到目标目录使用的函数
  15. if (move_uploaded_file($tempFileName, $destFileName)) {
  16. echo "我是".__FUNCTION__."方法"."<br>";
  17. // 预览
  18. echo "<img src='{$destFileName}' width='200'>";
  19. }
  20. }
  21. }
  22. //多文件上传
  23. public function two(){
  24. if ($_FILES['pic'])
  25. echo "我是".__FUNCTION__."方法"."<br>";
  26. foreach ($_FILES['pic']['error'] as $key => $error) {
  27. // 只要判断 error === 0
  28. if ($error === UPLOAD_ERR_OK) {
  29. // 临时文件名
  30. $tmpFileName = $_FILES['pic']['tmp_name'][$key];
  31. // 原始文件名
  32. $originalFileName = $_FILES['pic']['name'][$key];
  33. // 目标文件名
  34. $destFileName = 'uploads/'. $originalFileName;
  35. // 移动文件
  36. move_uploaded_file($tmpFileName, $destFileName);
  37. // 预览
  38. echo "<img src='{$destFileName}' width='200'>";
  39. }
  40. }
  41. }
  42. }
  43. $uploads = new Upload();
  44. //判断传入的数据
  45. $files = $_FILES['pic']['name'];
  46. if(!is_array($files)){
  47. $uploads->one();
  48. }else{
  49. $uploads->two();
  50. }
  51. ?>

4.2、前端代码——单文件上传

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>单文件上传</title>
  6. </head>
  7. <body>
  8. <form action="class.php" method="POST" enctype="multipart/form-data">
  9. <fieldset>
  10. <legend>单个文件上传</legend>
  11. <input type="hidden" name="MAX_FILE_SIZE" value="500000">
  12. <input type="file" name="pic" id="pic">
  13. <button>提交</button>
  14. </fieldset>
  15. </form>
  16. </body>
  17. </html>

4.3、前端代码——多文件上传

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>单文件上传</title>
  6. </head>
  7. <body>
  8. <form action="class.php" method="POST" enctype="multipart/form-data">
  9. <fieldset>
  10. <legend>单个文件上传</legend>
  11. <input type="hidden" name="MAX_FILE_SIZE" value="500000">
  12. <input type="file" name="pic[]" id="pic">
  13. <input type="file" name="pic[]" id="pic">
  14. <button>提交</button>
  15. </fieldset>
  16. </form>
  17. </body>
  18. </html>

学习总结

本节课我们学习了文件上传,通过本节课的学习使我学到了文件上传的知识以及如何进行文件上传的操作。希望在以后的实战中巩固和深入理解。

Correcting teacher:天蓬老师天蓬老师

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