PHP批量上傳一次點擊選中多個

來源:互聯網
上載者:User

標籤:

首先前端部分需要設定好控制項,這裡使用HTML5 中 input 的新增屬性 multiple 可以很好的解決了以往上傳多個需要點擊多次"上傳按鈕"的麻煩:

 

1 <form action="" method="post" enctype="multipart/form-data">2 3     <input type="file" id="upload_imgs" name="upload_imgs[]" multiple="multiple">4     5     <input type="submit" value="提交上傳">6 </form>

 

如果需要在 選中檔案後,提交前 告之使用者選中的檔案個數 可以使用files.length屬性,比如:

document.upload_imgs.files.length 即可擷取上述表單中的選中檔案個數。

 

接下來是PHP中的處理:

 1 if(!empty($_FILES)){ 2     //print_r($_FILES[‘upload_imgs‘]);exit; 3  4     function upload_files($name){ 5         $files =  $_FILES[$name]; 6  7         $k1 = $k2 = array(); 8         foreach($files as $key => $val){ 9             if($key == ‘tmp_name‘){10                 $k1 = $val;11             }12             if($key == ‘name‘){13                 $k2 = $val;14             }15         }16 17         //$filenames 儲存移動後的檔案名稱18         $filenames = array();19         foreach($k1 as $key => $val){20             //$rename 擷取原始檔案名 (不包括尾碼名)21             $rename = substr($k2[$key],0,strpos($k2[$key],‘.‘));22 23             //$suffix 檔案尾碼名24             $suffix = substr($k2[$key],strpos($k2[$key],‘.‘));25 26             //echo md5($rename) . $suffix;exit;27             $time = time();28             copy($val,‘./upload/‘ . md5($rename . $time) . $suffix);29 30             $filenames[] = ‘upload/‘ . md5($rename . $time) . $suffix;31         }32         print_r($filenames);exit;33 //        數組形式返回所上傳的檔案路徑34         return $filenames;35     }36 37 38     upload_files(‘upload_imgs‘);39 40 41 }

PHP檔案執行 upload_files() 函數後即可獲得上傳後的檔案路徑。

這樣批量上傳就做好了。

 

 

歡迎各種方式轉載,但請務必註明出處。

 

PHP批量上傳一次點擊選中多個

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.