Blogger Information
Blog 23
fans 0
comment 0
visits 18925
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP文件上传的各种处理
手机用户1617360551
Original
1069 people have browsed it

PHP文件上传的处理方法

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中$_FILES以二维数组形式保存:$_FILES[‘form_file_name’][‘key’];

form_file_name: 对应着表单中<input type="file" name="my_pic">中name属性值

key:共有 5 个键名, 描述如下:

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

处理文件上传方式

  1. <?php
  2. printf('<pre>%s</pre>',print_r($_FILES,true));
  3. $tmpFileName = $_FILES['my_pic']['tmp_name'];
  4. $originalFileName = $_FILES['my_pic']['name'];
  5. if(is_uploaded_file($tmpFileName))
  6. echo "{$originalFileName}:上传方式合法";
  7. ?>
  8. <!DOCTYPE html>
  9. <html lang="en">
  10. <head>
  11. <meta charset="UTF-8">
  12. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  13. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  14. <title>文件上传变量$_FILES</title>
  15. </head>
  16. <body>
  17. <!-- <form action="" method="post" enctype="application/x-www-form-urlencoded"> -->
  18. <form action="" method="post" enctype="multipart/form-data">
  19. <fieldset>
  20. <legend>单文件上传</legend>
  21. <input type="file" name="my_pic">
  22. <button>上传</button>
  23. </fieldset>
  24. </form>
  25. </body>
  26. </html>

PHP 文件上传的错误类型

文件上传错误信息描述

序号 常量 描述
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 文件写入失败

根据上传文件的错误类型判断

  1. <?php
  2. printf('<pre>%s</pre>',print_r($_FILES,true));
  3. $errorCode = $_FILES['my_pic']['error'];
  4. if($errorCode ===4 ) echo '没有文件上传';
  5. //两种方式检查
  6. //if(empty($_FILES['my_pic']['name'])) echo '没有文件传';
  7. ?>
  8. <!DOCTYPE html>
  9. <html lang="en">
  10. <head>
  11. <meta charset="UTF-8">
  12. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  13. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  14. <title>文件上传变量$_FILES</title>
  15. </head>
  16. <body>
  17. <!-- <form action="" method="post" enctype="application/x-www-form-urlencoded"> -->
  18. <form action="" method="post" enctype="multipart/form-data">
  19. <fieldset>
  20. <legend>单文件上传</legend>
  21. <input type="file" name="my_pic">
  22. <button>上传</button>
  23. </fieldset>
  24. </form>
  25. </body>
  26. </html>

处理文件类型是否符合要求 要求后缀是 ‘jpg’,’jpeg’,’png’,’wbmp’,’gif’

  1. <?php
  2. //$_FILES;
  3. printf('<pre>%s</pre>',print_r($_FILES,true));
  4. $fileType = $_FILES['my_pic']['type'];
  5. echo $fileType,'<br>';
  6. //print_r(explode('/',$fileType)[0]);
  7. echo strstr($fileType,'/',true);
  8. echo '<br>';
  9. $allow = ['jpg','jpeg','png','wbmp','gif'];
  10. $extension = pathinfo($_FILES['my_pic']['name'])['extension'];
  11. echo $extension;
  12. if(!in_array($extension,$allow)){echo '文件类型不正确';}
  13. else{
  14. echo '合法的文件类型';
  15. }
  16. //if(strstr($fileType,'/',true) !== 'image')
  17. //echo '<p>文件类型错误</p>';
  18. ?>
  19. <!DOCTYPE html>
  20. <html lang="en">
  21. <head>
  22. <meta charset="UTF-8">
  23. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  24. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  25. <title>文件上传变量$_FILES</title>
  26. </head>
  27. <body>
  28. <hr>
  29. <!-- <form action="" method="post" enctype="application/x-www-form-urlencoded"> -->
  30. <form action="" method="post" enctype="multipart/form-data">
  31. <fieldset>
  32. <legend>单文件上传:检测文件类型</legend>
  33. <input type="file" name="my_pic">
  34. <button>上传</button>
  35. </fieldset>
  36. </form>
  37. </body>
  38. </html>

检查文件上传内容不超过5M

用ini_get(‘uplond_max_filesize’)检测文件上传的最大值,将配置文件里的上传最大值改为5M;

  1. <?php
  2. printf('<pre>%s</pre>',print_r($_FILES,true));
  3. echo ini_get('upload_max_filesize');
  4. ini_set('upload_max_filesize','5M');
  5. ?>
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8">
  10. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  12. <title>文件上传变量$_FILES</title>
  13. </head>
  14. <body>
  15. <!-- <form action="" method="post" enctype="application/x-www-form-urlencoded"> -->
  16. <form action="" method="post" enctype="multipart/form-data">
  17. <fieldset>
  18. <legend>单文件上传</legend>
  19. <input type="hidden" name="MAX_FILE_SIZE" value="5524880">
  20. <input type="file" name="my_pic">
  21. <button>上传</button>
  22. </fieldset>
  23. </form>
  24. </body>
  25. </html>

PHP文件上传,多文件上传:逐个上传

  1. <?php
  2. printf('<pre>%s</pre>',print_r($_FILES,true));
  3. foreach ($_FILES as $file) {
  4. if($file['error'] == 0)
  5. {
  6. $destFile = 'upload/'.$file['name'];
  7. move_uploaded_file($file['tmp_name'],$destFile);
  8. echo "<img src='{$destFile}' width ='150'>";
  9. }
  10. }
  11. ?>
  12. <!DOCTYPE html>
  13. <html lang="en">
  14. <head>
  15. <meta charset="UTF-8">
  16. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  17. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <title>文件上传变量$_FILES</title>
  19. </head>
  20. <body>
  21. <!-- <form action="" method="post" enctype="application/x-www-form-urlencoded"> -->
  22. <form action="" method="post" enctype="multipart/form-data">
  23. <fieldset>
  24. <legend>多文件上传:逐个上传</legend>
  25. <input type="hidden" name="MAX_FILE_SIZE" value="5524880">
  26. <input type="file" name="my_pic1">
  27. <input type="file" name="my_pic2">
  28. <input type="file" name="my_pic3">
  29. <button>上传</button>
  30. </fieldset>
  31. </form>
  32. </body>
  33. </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