批量上傳方法也很簡單我們只要在給form時加個name=/"pictures[]/" 數組,然后再利用foreach ($_FILES[/"pictures/"][/"error/"] as $key => $error)對數組循環判斷,再利用move_uploaded_file($tmp_name, $uploadfile); 實現文件上傳就可以了.
html實例代碼如下:
- <html>
- <head><title>upload picture more once</title></head>
- <body>
- <form action="" method="post" enctype="multipart/form-data">
- <p>Pictures:<br />
- <input type="file" name="pictures[]" /><br />
- <input type="file" name="pictures[]" /><br />
- <input type="file" name="pictures[]" /><br />
- <input type="submit" name="upload" value="Send" />
- </p>
- </form>
- </body>
- </html>
php處理實例代碼如下:
- <?php
- if($_POST['upload']=='Send'){
- $dest_folder = "picture/";
- if(!file_exists($dest_folder)){
- mkdir($dest_folder);
- }
- foreach ($_FILES["pictures"]["error"] as $key => $error) {
- if ($error == UPLOAD_ERR_OK) {
- $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
- $name = $_FILES["pictures"]["name"][$key];
- $uploadfile = $dest_folder.$name;
- move_uploaded_file($tmp_name, $uploadfile);
- }
- }
- }
- ?>
方法相當簡單我們只是把單文件上傳改成了多文件上傳,其它只改了二個地方一個是表單名改成php數組形式一種是上傳文件地方利用foreach來對數組遍歷了.
新聞熱點
疑難解答