當前位置:才華齋>計算機>php語言>

php使用html5實現多檔案上傳例項

php語言 閱讀(3.07W)

在html沒有出來之前,要實現php多檔案上傳比較麻煩,需要在form表單裡面新增多個input file域。html5釋出以後,我們可以使用input file的html5屬性multiple來實現多檔案上傳,需要的`朋友可以參考下。

php使用html5實現多檔案上傳例項

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

  定義和用法:

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

  例項

<form action="demo_" 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_" 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['upload']['name']); $i++) {

//Get the temp file path

$tmpFilePath = $_FILES['upload']['tmp_name'][$i];

//Make sure we have a filepath

if ($tmpFilePath != ""){

//Setup our new file path

$newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];

//Upload the file into the temp dir

if(move_uploaded_file($tmpFilePath, $newFilePath)) {

//Handle other code here

}

}

}

最後,非常感謝大家的閱讀,希望能幫助到大家!