php多檔案上傳功能實現原理及代碼_php技巧

來源:互聯網
上載者:User
今天對多圖片上傳功能小小的研究了一下,把下面的代碼整理出來,方便以後自己使用以及供大家交流
1、upload.html頁面,即先是input type=file的檔案:
複製代碼 代碼如下:

<html>
<head>
<title></title>
</head>
<body>
<form enctype="multipart/form-data" action="do_upload.php" method="POST">
<!--表單中enctype="multipart/form-data"的意思,是設定表單的MIME編碼。預設情況,這個編碼格式是application/x-www-form-urlencoded,不能用於檔案上傳;只有使用了multipart/form- data,才能完整的傳遞檔案資料,進行下面的操作.-->
<fieldset>
<legend>圖片上傳</legend>
第一張圖片<input name="userfile" type="file"><br>
第二張圖片<input name="userfile" type="file"><br>
第三張圖片<input name="userfile" type="file"><br>
第四張圖片<input name="userfile" type="file"><br>
第五張圖片<input name="userfile" type="file"><br>
<!--數組的形式傳遞資料-->
<input type="submit" value="Send File">
</fieldset>
</form>
</body>
</html>

2、do_upload.php頁面,即處理多檔案上傳的頁面:
複製代碼 代碼如下:

<?php
//全域數組$_FILES
//$_FILES['userfile']['tmp_name']檔案在web伺服器中臨時儲存的位置
//$_FILES['userfile']['name']使用者系統中的檔案名稱
//$_FILES['userfile']['size']檔案的位元組大小
//$_FILES['userfile']['type']檔案的MIME類型,text/plain,image/gif
//$_FILES['userfile']['error']與檔案上傳相關的錯誤碼
?>
<?php
//用for迴圈來擷取傳遞的資料,是一個三維資料
for ($i=0;$i<count($_FILES['userfile']['tmp_name']);$i++)
{
$upfile=$new_folder."/".$_FILES['userfile']['name'][$i];//此處可以根據自己的需要修改
if(move_uploaded_file($_FILES['userfile']['tmp_name'][$i],$upfile)){
echo "第".($i+1)."張圖片上傳成功<br>";
}
else{
echo "第".($i+1)."張圖片上傳不了<br>";
}
}
?>

聯繫我們

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