php多個檔案上傳

技術小牛人發表於2017-11-14
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
<?php
for($i=1;$i<=5;$i++){
    if(!empty($_FILES["pic$i"][`name`])){   //如果檔案不為空
        if($_FILES["pic$i"][`error`]>0){//如果檔案上傳失敗
            switch ($_FILES["pic$i"][`error`]){
                case 1:
                    echo "上傳檔案大小超出配置檔案規定值";
                    break;
                case 2:
                    echo "上傳檔案大小超出表單中約定的值";
                    break;
                case 3:
                    echo "上傳檔案不全";
                    break;
                case 4:
                    echo "沒有上傳檔案";
                    break;
            }
        }else{
            if(!is_dir("upfile")){
                mkdir("upfile");
            }
            //獲取檔案型別
            list($maintype,$subtype)=explode("/",$_FILES["pic$i"][`type`]);
            //判斷檔案格式是否正確
            if ($maintype!="image"||$subtype!="pjpeg"){
                echo "上傳檔案格式不對!<br/>";
            }else{
                //如果上傳檔案格式正確,構建動態路徑
                $path "upfile/".rand(0, 10000).time().strtolower(strstr($_FILES["pic$i"][`name`], "."));
                //判斷是否上傳檔案
                if(is_uploaded_file($_FILES["pic$i"][`tmp_name`])){
                    //判斷檔案是否上傳成功
                    if(move_uploaded_file($_FILES["pic$i"][`tmp_name`], $path)){
                        echo "檔案:<span class=`STYLE1`>".time().strtolower(strstr($_FILES["pic$i"][`name`], "."))
                        ."</span> 上傳成功,大小為:<span class=`STYLE1`>".$_FILES["pic$i"][`size`]."</span>位元組<br/>";
                    }else{
                        echo "上傳失敗";
                    }
                }else{
                    echo "上傳檔案:<span class=`STYLE1`>".$_FILES["pic$i"][`name`]."</span>不合法!";
                }
            }
        }
    }else{
        continue;
    }
}
?>
<form action="07.php" method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
    <input type="file" name="pic1"/><br/>
    <input type="file" name="pic2"/><br/>
    <input type="file" name="pic3"/><br/>
    <input type="file" name="pic4"/><br/>
    <input type="file" name="pic5"/><br/>

    <input type="submit" value="上傳"/>
</form>


本文轉自  wbb827  51CTO部落格,原文連結:http://blog.51cto.com/wbb827/1325283


相關文章