2014-08-29 132 views
-1

我正努力将多张图片发布到新生成的目录中。有人能帮我吗?我已经缩小的问题,它不move_uploaded_files事实...PHP - 未移动到目录的文件

代码:

<?php 
    //Basic types 
    //Create Subdirectory 
     //Set the subdirectory name 
     $subdir = $_POST['folderName']; 
     //set the directory path name 
     $dir = ("./uploads/" . $subdir); 
     //make the directory 
     if(mkdir($dir, 0777)){ 
     } 

    foreach ($_FILES['file']['name'] as $f => $name) { 
    $allowedExts = array("gif", "jpeg", "jpg", "png"); 
    $temp = explode(".", $name); 
    $extension = end($temp); 
    //Set file type and size 
    if ((($_FILES['file']['type'][$f] == "image/gif") 
    || ($_FILES['file']['type'][$f] == "image/jpeg") 
    || ($_FILES['file']['type'][$f] == "image/jpg") 
    || ($_FILES['file']['type'][$f] == "image/png")) 
    && ($_FILES['file']['size'][$f] < 2000000) 
    && in_array($extension, $allowedExts)) 
    { 
     if ($_FILES['file']['error'][$f] > 0){ 
     echo "Return Code: " . $_FILES['file']['error'][$f] . "<br>"; 
     } else { 
     //if the file exists within the directory 
     if (file_exists($dir . $name)){ 
      echo "<p>File Already Exists</p>"; 
     } else { 
      $names = $_FILES['file']['name'][$f]; 

      //move the files you upload into the newly generated folder. 
      if (move_uploaded_file($names, "$dir/$name")){ 
      echo "<p>Moved</p>"; 
      } else { 
      echo "<p>not moved</p>"; 
      } 
      //send the file path to the database. 

      echo "<meta http-equiv='refresh' content='2;url=test.php'>"; 
     } 
     } 
    } else { 
     $error = "Invalid file"; 
    } 
    } 
?> 

<body> 
<form action="test.php" method="post" enctype="multipart/form-data"> 

    <p>Enter the name of Folder:</p> 
    <input type="text" name="folderName"> 

    <p>File:</p> 
    <input type="file" name="file[]" id="file" multiple="multiple" > 

    <input type="submit" value="Upload"> 
</form> 

<?php 
    $path = "./uploads/"; 

    $dir = opendir($path) or die ("unable to open directory"); 

    while ($file = readdir($dir)){ 
    if($file == "." || $file == ".." || $file == ".DS_Store" || $file == "test.php" || $file == "testCreate.php" || $file == "testDeleteDir.php"){ 
     continue; 
    } 

    echo "<a href='$path$file'>$file</a><a href='testDeleteDir.php?dir=$file'> Delete</p><br />"; 
    } 

    closedir($dir); 
?> 


</body> 

基本上,我的代码生成的文件夹/上传中的一个新的子文件夹。我试图访问该文件夹并将我上传的图片移动到那里,但它并没有做到这一点。谁来帮帮我?

谢谢!任何帮助表示赞赏。

+0

你确定创建的文件夹? – RajivRisi 2014-08-29 19:23:16

+0

脱离主题,但两点1)如果有人发布文件夹名称“../”会发生什么? - 你会想要清理你的用户输入2)也许考虑使用'$ path = $ _FILES ['image'] ['name']; $ ext = pathinfo($ path,PATHINFO_EXTENSION);'获取文件扩展名。 – CarCzar 2014-08-29 19:25:43

+0

@RajivRisi该文件夹创建在所需的位置。问题是链接到要上传的文件夹。 – 2014-08-29 19:29:45

回答

0
$names = $_FILES['file']['name'][$f]; 

应该

$names = $_FILES['file']['tmp_name'][$f]; 
+0

谢谢!我最后一次拥有它,但它不起作用,现在它已经做到了! LIFE SAVER – 2014-08-29 19:34:50

+0

很高兴它的工作:) – 2014-08-29 19:38:11