Blogger Information
Blog 33
fans 0
comment 0
visits 27676
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
服务端 - PHP - 文件操作之文件上传
Original
724 people have browsed it

服务端 - PHP - 文件操作之文件上传

一、常见的文件上传配置项

序号 配置项 默认值 描述
1 file_uploads On 使 PHP 支持文件上传
2 upload_tmp_dir /tmp 指示应该临时把上传的文件存储在什么位置
3 max_file_uploads 20 单次请求时允许上传的最大文件数量
4 max_execution_time 30 设置 PHP 最长执行时间(秒)
5 max_input_time 60 设置 PHP 通过 POST/GET/PUT 接收数据的时长(秒)
6 memory_limit 128M 系统分配给当前脚本执行可用的最大内存容量
7 post_max_size 8M 允许的 POST 数据的总大小(以字节为单位)
8 upload_max_filesize 32M 允许的尽可能最大的文件上传(以字节为单位)

二、$_FILES全局变量

1. 5个属性

序号 键名 描述
1 name 文件在客户端的原始文件名(即存在用户电脑上的文件名)
2 type 文件的 MIME 类型, 由浏览器提供, PHP 并不检查它
3 tmp_name 文件被上传到服务器上之后,在临时目录中临时文件名
4 error 和该文件上传相关的错误代码
5 size 已上传文件的大小(单位为字节)

2. 文件上传错误信息描述

序号 常量 描述
1 UPLOAD_ERR_OK 0 没有错误发生,文件上传成功
2 UPLOAD_ERR_INI_SIZE 1 文件超过php.iniupload_max_filesize
3 UPLOAD_ERR_FORM_SIZE 2 文件大小超过表单中MAX_FILE_SIZE指定的值
4 UPLOAD_ERR_PARTIAL 3 文件只有部分被上传
5 UPLOAD_ERR_NO_FILE 4 没有文件被上传
6 UPLOAD_ERR_NO_TMP_DIR 6 找不到临时文件夹
7 UPLOAD_ERR_CANT_WRITE 7 文件写入失败

3. 支持文件上传的前端表单设置

序号 属性设置 描述
1 <form method="POST"> 请求类型必须是POST
2 <form enctype="multipart/form-data"> 设置表单提交数据的编码类型
3 <input type="file" name="uploads"> 设置表单控件类型与名称以支持上传
4 <input type="hidden" name="MAX_FILE_SIZE" value="..."> 设置隐藏域限制上传文件大小(可选)

4. 设置表单数据,在发送到服务器之前的编码规则

序号 属性值 描述
1 application/x-www-form-urlencoded 默认值, 发送前进行编码,空格转+,非空字符转 16 进制
2 multipart/form-data 不对字符编码,以二进制发送,适合文件上传
3 text/plain 纯文本发送,仅对空格编码(转为+)

三、实现

1. 单文件上传

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>单文件上传</title>
  7. </head>
  8. <body>
  9. <div class="a-box">
  10. <form action="" method="POST" enctype="multipart/form-data">
  11. <fieldset>
  12. <!--在文件上传至服务器之前对文件大小进行限制-->
  13. <input type="hidden" name="MAX_FILE_SIZE" value="300000">
  14. <input type="file" name="pic">
  15. <button>上传</button>
  16. </fieldset>
  17. </form>
  18. </div>
  19. </body>
  20. </html>
  21. <?php
  22. //1. 自定义上传异常类
  23. class UploadException extends Exception {
  24. public function __toString()
  25. {
  26. return <<< UPLOAD
  27. <p>代码:$this->code; 错误信息:$this->message; 文件:$this->file; 行:$this->line</p>
  28. UPLOAD;
  29. }
  30. };
  31. //2. 进行异常处理
  32. try {
  33. //2.1 获取错误代码
  34. $error_code = $_FILES['pic']['error'];
  35. //2.2 根据错误代码作异常处理
  36. if ($error_code > UPLOAD_ERR_OK) {
  37. switch ($error_code) {
  38. case 1:
  39. throw new UploadException('文件大小超过服务端限制', 1);
  40. break;
  41. case 2:
  42. throw new UploadException('文件大小超过客户端限制', 2);
  43. break;
  44. case 3:
  45. throw new UploadException('文件只有部分被上传', 3);
  46. break;
  47. case 4:
  48. throw new UploadException('文件上传失败', 4);
  49. break;
  50. case 6:
  51. throw new UploadException('找不到临时文件夹', 6);
  52. break;
  53. case 7:
  54. throw new UploadException('文件写入失败', 7);
  55. break;
  56. default:
  57. throw new UploadException('未知类型错误', 8);
  58. }
  59. }
  60. //2.3 判断文件类型
  61. $file_type = $_FILES['pic']['type'];
  62. if (strstr($file_type, '/', true) !== 'image') {
  63. throw new UploadException('文件类型错误', 9);
  64. }
  65. //3. 移动文件到目标目录
  66. //3.1 获取临时文件名
  67. $tmp_file_name = $_FILES['pic']['tmp_name'];
  68. //3.2 获取原始文件名
  69. $ori_file_name = $_FILES['pic']['name'];
  70. //3.3 创建目录文件名
  71. $dest_file_name = 'uploads/'.md5(time().mt_rand(1,1000)).strstr($ori_file_name, '.');
  72. if (move_uploaded_file($tmp_file_name, $dest_file_name)) {
  73. echo '<script>alert("文件上传成功");</script>';
  74. //4. 预览
  75. echo "<img src='$dest_file_name' width='200'>";
  76. }
  77. } catch (UploadException $e) {
  78. echo $e;
  79. }
  80. ?>





2. 多文件上传

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>多文件上传</title>
  7. </head>
  8. <body>
  9. <div class="a-box">
  10. <form action="" method="POST" enctype="multipart/form-data">
  11. <fieldset>
  12. <!--在文件上传至服务器之前对文件大小进行限制-->
  13. <input type="hidden" name="MAX_FILE_SIZE" value="300000">
  14. <input type="file" name="pic[]">
  15. <input type="file" name="pic[]">
  16. <input type="file" name="pic[]">
  17. <input type="file" name="pic[]">
  18. <button>上传</button>
  19. </fieldset>
  20. </form>
  21. </div>
  22. </body>
  23. </html>
  24. <?php
  25. //1. 自定义上传异常类
  26. class UploadException extends Exception {
  27. public function __toString()
  28. {
  29. return <<< UPLOAD
  30. <p>代码:$this->code; 错误信息:$this->message; 文件:$this->file; 行:$this->line</p>
  31. UPLOAD;
  32. }
  33. };
  34. //2. 进行异常处理
  35. try {
  36. if ($_FILES['pic']) {
  37. //取出错误码
  38. foreach ($_FILES['pic']['error'] as $key => $error_code) {
  39. if ($error_code > UPLOAD_ERR_OK) {
  40. switch ($error_code) {
  41. case 1:
  42. throw new UploadException('文件大小超过服务端限制', 1);
  43. break;
  44. case 2:
  45. throw new UploadException('文件大小超过客户端限制', 2);
  46. break;
  47. case 3:
  48. throw new UploadException('文件只有部分被上传', 3);
  49. break;
  50. case 4:
  51. throw new UploadException('文件上传失败', 4);
  52. break;
  53. case 6:
  54. throw new UploadException('找不到临时文件夹', 6);
  55. break;
  56. case 7:
  57. throw new UploadException('文件写入失败', 7);
  58. break;
  59. default:
  60. throw new UploadException('未知类型错误', 8);
  61. }
  62. } else {
  63. //判断文件类型
  64. foreach ($_FILES['pic']['type'] as $file_type) {
  65. if (strstr($file_type, '/', true) !== 'image') {
  66. throw new UploadException('文件类型错误', 9);
  67. }
  68. }
  69. }
  70. //3. 移动文件
  71. if ($error_code === 0) {
  72. $tmp_file_name = $_FILES['pic']['tmp_name'][$key];
  73. $ori_file_name = $_FILES['pic']['name'][$key];
  74. $dest_file_name = 'uploads/'.md5(time().mt_rand(1, 1000)).strstr($ori_file_name, '.');
  75. if (move_uploaded_file($tmp_file_name, $dest_file_name)) {
  76. //4. 预览
  77. echo "<img src='$dest_file_name' width='200'>";
  78. }
  79. }
  80. };
  81. echo '<script>alert("文件上传成功");</script>';
  82. }
  83. } catch (Exception $e) {
  84. echo $e;
  85. }
  86. ?>



3. 批量上传

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>批量上传</title>
  7. </head>
  8. <body>
  9. <div class="a-box">
  10. <form action="" method="POST" enctype="multipart/form-data">
  11. <fieldset>
  12. <!--在文件上传至服务器之前对文件大小进行限制-->
  13. <input type="hidden" name="MAX_FILE_SIZE" value="300000">
  14. <input type="file" name="pic[]" multiple>
  15. <button>上传</button>
  16. </fieldset>
  17. </form>
  18. </div>
  19. </body>
  20. </html>
  21. <?php
  22. //1. 自定义上传异常类
  23. class UploadException extends Exception {
  24. public function __toString()
  25. {
  26. return <<< UPLOAD
  27. <p>代码:$this->code; 错误信息:$this->message; 文件:$this->file; 行:$this->line</p>
  28. UPLOAD;
  29. }
  30. };
  31. //2. 进行异常处理
  32. try {
  33. if ($_FILES['pic']) {
  34. //取出错误码
  35. foreach ($_FILES['pic']['error'] as $key => $error_code) {
  36. if ($error_code > UPLOAD_ERR_OK) {
  37. switch ($error_code) {
  38. case 1:
  39. throw new UploadException('文件大小超过服务端限制', 1);
  40. break;
  41. case 2:
  42. throw new UploadException('文件大小超过客户端限制', 2);
  43. break;
  44. case 3:
  45. throw new UploadException('文件只有部分被上传', 3);
  46. break;
  47. case 4:
  48. throw new UploadException('文件上传失败', 4);
  49. break;
  50. case 6:
  51. throw new UploadException('找不到临时文件夹', 6);
  52. break;
  53. case 7:
  54. throw new UploadException('文件写入失败', 7);
  55. break;
  56. default:
  57. throw new UploadException('未知类型错误', 8);
  58. }
  59. } else {
  60. //判断文件类型
  61. foreach ($_FILES['pic']['type'] as $file_type) {
  62. if (strstr($file_type, '/', true) !== 'image') {
  63. throw new UploadException('文件类型错误', 9);
  64. }
  65. }
  66. }
  67. //3. 移动文件
  68. if ($error_code === 0) {
  69. $tmp_file_name = $_FILES['pic']['tmp_name'][$key];
  70. $ori_file_name = $_FILES['pic']['name'][$key];
  71. $dest_file_name = 'uploads/'.md5(time().mt_rand(1, 1000)).strstr($ori_file_name, '.');
  72. if (move_uploaded_file($tmp_file_name, $dest_file_name)) {
  73. //4. 预览
  74. echo "<img src='$dest_file_name' width='200'>";
  75. }
  76. }
  77. };
  78. echo '<script>alert("文件上传成功");</script>';
  79. }
  80. } catch (Exception $e) {
  81. echo $e;
  82. }
  83. ?>



四、课程总结

  • 今天学习了 PHP 的文件上传,通过上课认真听讲和认真完成老师布置的作业,使得我对 PHP 文件操作的理解和运用更加深入和熟悉。最主要的知识点是明白和掌握了文件上传的特点以及它的基本用法。
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!