2015-01-14 45 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

相关问题