老师看一下这是什么情况?

老师看一下这是什么情况?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
 
$arr = [];
$mimes = ["image/jpeg","image/png","image/gif"];
$exts = ["jpeg","png","jpg","gif"];
$dir "uploads";
 
foreach ($_FILES as $key => $val){
    //接受数据
    $name $_FILES[$key]['name'];
    $type $_FILES[$key]['type'];
    $tmp_name $_FILES[$key]['tmp_name'];
    $error $_FILES[$key]['error'];
    $size $_FILES[$key]['size'];
 
    //错误检查
    if ($error>0){
        switch ($error){
            case 1:
                $arr[$key]= "$name.上传文件超出了upload_max_filesize";
                continue 2;
            case 2:
                $arr[$key]= "$name.上传文件超出了表单的MAX_FILE_SIZE";
                continue 2;
            case 3:
                $arr[$key]= "$name.文件只有部分被上传";
                continue 2;
            case 4:
                $arr[$key]= "没有文件被上传";
                continue 2;
            case 6:
                $arr[$key]= "$name.找不到临时目录";
                continue 2;
            case 7:
                $arr[$key]= "$name.写入磁盘失败";
                continue 2;
            case 8:
                $arr[$key]= "$name.上传的文件被PHP扩展程序中断";
                continue 2;
            default:
                $arr[$key]= "$name.未知错误";
                continue 2;
        }
    }
    //检测MIME类型
    if (!in_array($type,$mimes)){
        $arr[$key] = "文件的类型{$type}不允许";
        continue;
    }
    //检测扩展名是否允许
    $ext pathinfo($name,PATHINFO_EXTENSION);
    if (!in_array($ext,$exts)){
        $arr[$key] = "文件扩展名{$ext}不支持上传";
        continue;
    }
    //生成随机文件名
    $fileName = md5(uniqid(microtime(),true)).".".$ext;
 
    //检测文件上传方式
    if (!is_uploaded_file($tmp_name)){
        $arr[$key] = "文件非法上传";
        continue;
    }
    //移动文件
    if (!is_dir($dir)){
        mkdir($dir,0777,true);
    }
    if (!move_uploaded_file($tmp_name,$dir.DIRECTORY_SEPARATOR.$fileName)){
        echo $name."文件上传失败";
        continue;
    }
}
 
if (count($arr)>0){
    var_dump($arr);
}else{
    echo "文件全部上传成功";
}


正在回答

登陆购买课程后可参与讨论,去登陆

2回答

您好,判断$_FILES是否为空,如果上传文件大于upload_max_filesize的大小,那么$_FILES取得的是空数组。祝学习愉快!

提问者 雨之赞歌 2019-03-21 10:17:35

上传超过了upload_max_filesize的文件会报这个错误,怎么还上传成功呢?

http://img1.sycdn.imooc.com//climg/5c92f42b0001255a15020184.jpg

  • 提问者 雨之赞歌 #1
    没有关于post_max_size的错误号,那如果出现这个问题该怎么处理呢?
    2019-03-21 10:29:35
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码