php 批量上傳檔案

來源:互聯網
上載者:User

form.php

<html><body><form action="upload.php" method="post" enctype="multipart/form-data"><label for="file">上傳:</label><input type="file" name="file[]"/> <input type="file" name="file[]"/> <input type="submit" name="submit" value="上傳" /></form></body></html>


upload.php

//設定上傳檔案大小限制(單位b)$max_size=500000;//設定上傳檔案的檔案格式限制$format=array("image/jpeg","image/gif","image/png");//檔案上傳目錄$dir=dirname(__FILE__) ."/upload/";//判斷上傳目錄,不存在就建立if(!is_dir($dir)){    mkdir($dir,true);}//批量上傳檔案for($i=0,$j=count($_FILES["file"]["name"]);$i<$j;$i++){    //被上傳檔案的名稱    $name=$_FILES["file"]["name"][$i];    //被上傳檔案的類型    $type=$_FILES["file"]["type"][$i];    //被上傳檔案的大小,以位元組計    $size=$_FILES["file"]["size"][$i];    //儲存在伺服器的檔案的臨時副本的名稱    $tmp_name=$_FILES["file"]["tmp_name"][$i];    //由檔案上傳導致的錯誤碼    $error=$_FILES["file"]["error"][$i];        //判斷檔案大小    if($size>$max_size){        exit("檔案大小超出最大值");    }    //判斷檔案格式    if(!in_array($type,$format)){        exit("無效的檔案格式");    }         //組建檔案名    date_default_timezone_set("PRC");    $file_name=time().mt_rand(1111, 999999);    //擷取檔案格式    $ext=substr($type, strpos($type, "/")+1);        if($error>0){        exit($error);    }else{        if(move_uploaded_file($tmp_name, $dir.$file_name.".".$ext)){           //exit("上傳成功");         }    }}





















聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.