首頁  >  文章  >  後端開發  >  php 使用html5實作多檔案上傳實例

php 使用html5實作多檔案上傳實例

高洛峰
高洛峰原創
2017-01-14 14:08:361624瀏覽

先跟大家介紹一下html5中file的multiple屬性

定義和用法

multiple 屬性規定輸入欄位可選擇多個值。如果使用該屬性,則欄位可接受多個值。

實例:

<form action="demo_form.asp" method="get">
 Select images: <input type="file" name="img" multiple="multiple" />
 <input type="submit" />
</form>

   

上面實例中的input file 可接受多個檔案上傳欄位。

了解了html5中file的multiple屬性,下面我們開始講解使用html5實作多檔案上傳。

實例代碼:

html:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form action="my_parser.php" method="post" enctype="multipart/form-data">
 <p><input name="upload[]" type="file" multiple="multiple" /></p>
 <input type="submit" value="Upload all files">
</form>
</body>
</html>

   

php代碼:

for($i=0; $i<count($_FILES[&#39;upload&#39;][&#39;name&#39;]); $i++) {
 //Get the temp file path
 $tmpFilePath = $_FILES[&#39;upload&#39;][&#39;tmp_name&#39;][$i];
 
 //Make sure we have a filepath
 if ($tmpFilePath != ""){
  //Setup our new file path
  $newFilePath = "./uploadFiles/" . $_FILES[&#39;upload&#39;][&#39;name&#39;][$i];
 
  //Upload the file into the temp dir
  if(move_uploaded_file($tmpFilePath, $newFilePath)) {
 
   //Handle other code here
 
  }
 }
}

更多php 使用html5實作多檔案上傳實例相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn