Blogger Information
Blog 145
fans 7
comment 7
visits 164610
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
02月19日作业:单文件和多文件上传
李东亚¹⁸⁰³⁹⁵⁴⁰¹²⁰
Original
629 people have browsed it

作业一(单文件上传(仅限pdf上传))

目录

1、html代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>上传文件</title>
  6. </head>
  7. <body>
  8. <form action="demo.php" method="POST" enctype="multipart/form-data">
  9. <fieldset>
  10. <legend>头像上传</legend>
  11. <input type="hidden" name='MAX_FILE_SIZE' value=''>
  12. <input type="file" name='user_pic'>
  13. <button>上传</button>
  14. </fieldset>
  15. </form>
  16. </body>
  17. </html>

2、demo.php代码:

  1. <?php
  2. // phpinfo();
  3. // print_r($_FILES);
  4. try{
  5. $error_code=$_FILES['user_pic']['error'];
  6. // echo $error_code.'<br>';
  7. // var_dump($error_code);
  8. // echo UPLOAD_ERR_CANT_WRITE;
  9. if ($error_code > UPLOAD_ERR_OK){
  10. switch($error_code){
  11. case UPLOAD_ERR_INI_SIZE:
  12. throw new Exception('文件太大,超过服务器接收大小');
  13. break;
  14. case UPLOAD_ERR_FORM_SIZE:
  15. throw new Exception('文件大小超过网页限制大小');
  16. break;
  17. case UPLOAD_ERR_PARTIAL:
  18. throw new Exception('文件上传不完整');
  19. break;
  20. case UPLOAD_ERR_NO_TMP_DIR:
  21. throw new Exception('没有找到可以临时存储文件的目录文件');
  22. break;
  23. case UPLOAD_ERR_CANT_WRITE:
  24. throw new Exception('没有上传权限');
  25. break;
  26. default:
  27. throw new Exception('未知类型错误');
  28. }
  29. }else{
  30. $upfile=$_FILES['user_pic']['tmp_name'];
  31. // echo $upfile;
  32. $type='pdf';
  33. if (is_uploaded_file($upfile)){
  34. $filetype=pathinfo($_FILES['user_pic']['name'])['extension'];
  35. if ($filetype === $type){
  36. $newupname=__DIR__.'\uploadfile'.DIRECTORY_SEPARATOR.md5($_FILES['user_pic']['name']).time().'.'.$filetype;
  37. echo $newupname;
  38. if(move_uploaded_file($upfile,$newupname)){
  39. echo '<script>alert("上传成功");location.href="form.html";</script>';
  40. }
  41. }else{
  42. throw new Exception('文件类型不允许上传,请上传PDF格式');
  43. }
  44. }else{
  45. throw new Exception('上传后,文件非法错误');
  46. }
  47. }
  48. }catch (Exception $e){
  49. echo $e->getMessage();
  50. }

作业二(多文件上传,总上传文件不能大于10M(修改php.ini里面的post_max_size的值为10M即可),且只能传office文档)

1、html代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>上传文件</title>
  6. </head>
  7. <body>
  8. <form action="demo2.php" method="POST" enctype="multipart/form-data">
  9. <fieldset>
  10. <legend>头像上传</legend>
  11. <!-- <input type="hidden" name='MAX_FILE_SIZE' value=''> -->
  12. <input type="file" name='user_pic[]'>
  13. <input type="file" name='user_pic[]'>
  14. <input type="file" name='user_pic[]'>
  15. <!-- <input type="file" name='user_pic[]'> -->
  16. <button>上传</button>
  17. </fieldset>
  18. </form>
  19. </body>
  20. </html>

2、公共处理函数:代码

  1. <?php
  2. function verify($file){
  3. try{
  4. $error_code=$file[3];
  5. // echo $error_code.'<br>';
  6. // var_dump($error_code);
  7. // echo UPLOAD_ERR_CANT_WRITE;
  8. if ($error_code > UPLOAD_ERR_OK){
  9. switch($error_code){
  10. case UPLOAD_ERR_INI_SIZE:
  11. throw new Exception('文件太大,超过服务器接收大小');
  12. break;
  13. case UPLOAD_ERR_FORM_SIZE:
  14. throw new Exception('文件大小超过网页限制大小');
  15. break;
  16. case UPLOAD_ERR_PARTIAL:
  17. throw new Exception('文件上传不完整');
  18. break;
  19. case UPLOAD_ERR_NO_TMP_DIR:
  20. throw new Exception('没有找到可以临时存储文件的目录文件');
  21. break;
  22. case UPLOAD_ERR_CANT_WRITE:
  23. throw new Exception('没有上传权限');
  24. break;
  25. case UPLOAD_ERR_NO_FILE:
  26. throw new Exception('没有文件被上传');
  27. break;
  28. default:
  29. throw new Exception('未知类型错误');
  30. }
  31. }else{
  32. $upfile=$file[2];
  33. // echo $upfile;
  34. $type=['pdf','doc','txt','xls','ppt','docx','xlsx','pptx'];
  35. if (is_uploaded_file($upfile)){
  36. $filetype=pathinfo($file[0])['extension'];
  37. if (in_array($filetype,$type)){
  38. $newupname=__DIR__.'\uploadfile'.DIRECTORY_SEPARATOR.md5($file[0]).time().'.'.$filetype;
  39. // echo $newupname;
  40. if(move_uploaded_file($upfile,$newupname)){
  41. echo "{$file[0]}:文件上传成功";
  42. }
  43. }else{
  44. throw new Exception("{$file[0]}文件类型不允许上传,请上传office办公文档");
  45. }
  46. }else{
  47. throw new Exception('上传后,文件非法错误');
  48. }
  49. }
  50. }catch (Exception $e){
  51. echo $e->getMessage();
  52. }
  53. }

3、demo2.php代码:

  1. <?php
  2. require 'upfunction.php';
  3. // phpinfo();
  4. // print_r($_FILES);
  5. $offices=[];
  6. //
  7. foreach ($_FILES as $file) {
  8. // print_r($file);
  9. for($i=0;$i<count($file['name']);$i++) {
  10. $offices[]=[
  11. $file['name'][$i],
  12. $file['type'][$i],
  13. $file['tmp_name'][$i],
  14. $file['error'][$i],
  15. $file['size'][$i]
  16. ];
  17. }
  18. }
  19. $i=0;
  20. foreach ($offices as $office) {
  21. // print_r($office);
  22. verify($office);
  23. echo '<br>';
  24. }

作业三

1、知识点:

  • 关于上传php.ini中相关的设置:
    file_upload:On/Off是否允许文件上传
    upload_tmp_dir:文件上传的临时目录
    upload_max_filesize:单个文件上传的最大容量
    post_max_size:通过POST一次性上传数据的大小,至少要大于upload_max_filesize;
    max_file_uploads:单个请求,允许上传的最大的文件的数量
    2、超全局常量:$_FILES;
  1. [name] => user_pic.jpg
  2. [type] => image/jpeg
  3. [tmp_name] => /Applications/MAMP/tmp/php/phprqLCR1
  4. [error] => 0
  5. [size] => 20029

错误代码:

  • 0: UPLOAD_ERR_OK, 没有错误发生,文件上传成功
  • 1: UPLOAD_ERR_INI_SIZE, 上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值
  • 2: UPLOAD_ERR_FORM_SIZE, 上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值
  • 3: UPLOAD_ERR_PARTIAL, 文件只有部分被上传(网络临时中断,只有部分上传)
  • 4: UPLOAD_ERR_NO_FILE, 没有文件被上传
  • 6: UPLOAD_ERR_NO_TMP_DIR, 找不到临时文件目录(文件夹或改动,极少遇到,除非人为恶意)
  • 7: UPLOAD_ERR_CANT_WRITE, 文件写入失败, 上传目录无写的权限

3、phpinfo();查询php的版本信息;
4、pathinfo();

  1. Array
  2. (
  3. [dirname] => 0219
  4. [basename] => demo2.php
  5. [extension] => php
  6. [filename] => demo2
  7. )

5、in_array($value,$array):判断$value是否在$array中,返回布尔值;
6、is_uploaded_file($tmpFile):检测是否是通过POST方式上传的文件, 本质上就是检测临时文件是否是一个合法的文件;
7、move_uploaded_file($tmpFile, $destName): 把$tmpFile的临时文件移动到$destName文件中,一般要跟上绝对路径;防止找不到目标文件目录;
8、<script>alert("上传成功");location.href="form1.html";</script>弹窗提示,并返回指定页面;
10、html标签:

  1. <!-- action:处理上传脚本, method: 必须是POST, multipart/form-data:允许接收表单提交的文件 -->
  2. <form action="demo3.php" method="POST" enctype="multipart/form-data">
  3. <fieldset>
  4. <legend>头像上传</legend>
  5. <!-- 这个上传文件大小的限制字段,必须写到file字段的前面 -->
  6. <input type="hidden" name="MAX_FILE_SIZE" value="600">
  7. <input type="file" name="user_pic">
  8. <!-- <input type="file" name="user_pic1">
  9. <input type="file" name="user_pic2">
  10. <input type="file" name="user_pic3"> -->
  11. <button>上传</button>
  12. </fieldset>
  13. </form>
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