PHP 随笔3-1 上传多图

 1 <?php
 2     date_default_timezone_set('PRC');
 3 // 上传单张图片函数
 4 function uploads($name,$path,$info,$i){
 5     $error = $_FILES[$name]['error'][$i];
 6     // 错误信息处理
 7     if($error > 0){
 8         switch($error){
 9             case 1:
10             case 2:
11                 echo "第".($i+1)."文件太大!!";
12                 break;
13             case 3:
14                 echo "第".($i+1)."网络错误!!!";
15                 break;
16             case 4:
17                 echo "第".($i+1)."没有上传文件!!!";
18                 break;
19             case 6:
20                 echo "第".($i+1)."找不到文件!!!";
21                 break;
22             default:
23                 echo "第".($i+1)."其他错误!!!";
24                 break;
25         }
26         return 3;
27     }
28     
29     if(is_uploaded_file($_FILES[$name]['tmp_name'][$i])){
30         // 上传图片类型的数组
31         $typearr = array('image/jpeg','image/jpg','image/png','image/gif');
32         if(!in_array($_FILES[$name]['type'][$i],$typearr)){
33             echo "只能上传".implode(",",$typearr)."类型的文件";
34             die;
35         }
36             $ext = strstr($_FILES[$name]['name'][$i],'.');
37             $date = date("Y-m-d");
38             $pathinfo = $path.$date.'/';
39             // 创建目录
40             if(!is_dir($pathinfo)){
41                 mkdir($pathinfo);
42             }
43 
44             $newname = $pathinfo.date("His").mt_rand().$ext;
45             $newname1 = '第'.($i+1).'个:'.$newname;
46             // 写入上传图片的信息
47             file_put_contents($info,$newname1."\n",FILE_APPEND);
48             $res =  move_uploaded_file($_FILES[$name]['tmp_name'][$i],$newname);     
49             if($res){
50                 return 1;
51             }else{
52                 return 2;
53             }
54     }
55     
56 }
57 
58 // 上传多张图片函数
59 // 循环判断,一张张上传
60 function duotu($name,$path,$info){
61     if(isset($_POST) && $_FILES){
62         foreach($_FILES as $q){
63             foreach($q as $q1){
64                 foreach($q1 as $key => $q2){
65                     $q  = uploads($name,$path,$info,$key);
66                     if($q == 1){
67                         echo "<script>alert('上传成功!') ;location.replace('duotu.php');</script>";
68                     }else if($q == 2){
69                         echo "<script>alert('上传失败!!!!!');</script>";
70                     }
71                 }   
72             }
73         }
74     }
75 }
76     $name = 'photo';
77     $path = './duotu/';
78     $info = 'duotu.txt';
79     duotu($name,$path,$info);
80 ?>
81 <!DOCTYPE html>
82 <html lang="en">
83 <head>
84     <meta charset="UTF-8">
85     <meta name="viewport" content="width=device-width, initial-scale=1.0">
86     <meta http-equiv="X-UA-Compatible" content="ie=edge">
87     <title>Document</title>
88 </head>
89 <body>
90     <form action="duotu.php" method="post" enctype="multipart/form-data">
91         上传文件<input type="file" name="photo[]" id="" ><br>
92         上传文件<input type="file" name="photo[]" id="" ><br>
93         上传文件<input type="file" name="photo[]" id="" ><br>
94        <input type="hidden" name="MAX_FILE_SIZE" value='2M' ><br>
95         <input type="submit">
96     </form>
97 </body>
98 </html>
View Code

 

posted @ 2018-01-04 17:22  期待明天**  阅读(84)  评论(0)    收藏  举报