2015-01-14 46 views
-2

好吧,我可以成功上傳1個文件,使其在主文件夾內創建一個新文件夾。然後將文件放在那裏。如何上傳多個文件,創建文件夾並將文件放置在那裏PHP

但是我很難做多個文件到同一個文件夾。

我要上傳2個文件,看起來像這樣:

文檔/ 1 /然後將文件在這裏。

是什麼樣子:

文檔/ 1

HTML:

<form action="upload.php" method="post" enctype="multipart/form-data"> 
     Select image to upload:<br/> 
     <input type="file" name="fileToUpload" id="fileToUpload"><br/> 
     <input type="file" name="fileToUpload" id="fileToUpload"><br/> 
     <input type="submit" value="Upload Image" name="submit"> 
    </form> 

PHP:

<?php 
$number = 1; 
$target_dir = "docs/"; 
$new = mkdir($target_dir . $number . "/"); 
$target_file = $target_dir . $number . "/"; 

$uploadOk = 1; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
// Check if image file is a actual image or fake image 

    // Check if file already exists 
    if (file_exists($target_file)) { 
     echo "Sorry, file already exists."; 
     $uploadOk = 0; 
    } 
    // Check file size 
    if ($_FILES["fileToUpload"]["size"] > 500000) { 
     echo "Sorry, your file is too large."; 
     $uploadOk = 0; 
    } 
    // Allow certain file formats 
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" 
    && $imageFileType != "gif") { 
     echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
     $uploadOk = 0; 
    } 
    // Check if $uploadOk is set to 0 by an error 
    if ($uploadOk == 0) { 
     echo "Sorry, your file was not uploaded."; 
    // if everything is ok, try to upload file 
    } else { 
     foreach($_FILES['fileToUpload']['name'] as $file => $uploaded_file){ 
      move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file . $uploaded_file); 
     } 
} 
?> 

任何想法?

編輯工作:

 <input type="file" name="pictures[]" /><br/> 
     <input type="file" name="pictures[]" /><br/> 
     <input type="file" name="pictures[]" /><br/> 
     <input type="submit" value="Send" /> 

    </form> 

$target_dir = "docs/"; 
$dir=glob($target_dir."/*",GLOB_ONLYDIR); 
$number = count($dir) + 1; 
$new = mkdir($target_dir . $number . "/"); 
$target_file = $target_dir . $number . "/"; 


foreach ($_FILES["pictures"]["name"] as $key => $Name) 
{ 
     $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; 
     $name = $_FILES["pictures"]["name"][$key]; 
     move_uploaded_file($tmp_name, $target_file . "$name"); 
} 
+0

'我有麻煩了'麻煩怎麼表現出來? - 閱讀:更多信息pls; (也是你的文件輸入具有相同的名稱) – birdspider

+0

你有2個輸入字段具有相同的名稱。 –

+0

對不起,我打算取出$ new =即使它仍然創建1文件夾。 和我知道我有同樣的名字,我雖然這就是爲什麼我會通過他們的foreach? – user3620531

回答

1

首先改變你的文件輸入(注意,ID應該是唯一的所有元素,以避免JavaScript的問題):

<input type="file" name="fileToUpload[]" id="fileToUpload_0"> 

既然你要上傳的文件陣列。

foreach($_FILES['fileToUpload'][$name] as $index=>$uploaded_file) { 
    move_uploaded_file(
      $_FILES['fileToUpload']['tmp_name'][$index], 
     $target_file.$uploaded_file); 
} 

更新:曾在語言和文件處理邏輯一時的組合。

+0

感謝您的幫助,但是這沒有奏效。它仍然創建了文件夾1,但後來通過所有錯誤我檢查與$ uploadOk變量 – user3620531

+0

@ user3620531,它非常惱人,當人們說的答案「沒有工作。」。也許它並沒有解決整個問題,因爲你的代碼中有很多錯誤。這並不意味着它不起作用。你很幸運,任何人甚至發佈了答案,因爲「爲我調試我的代碼」的要求是無關緊要的。所以要更尊重。 – developerwjk

+0

掛上我很恭敬。我說謝謝你的幫助,我說它沒有解決問題,並說它做了什麼/ – user3620531

相關問題