PHP - 文件未移动到目录

时间:2014-08-29 19:15:52

标签: php

我努力将多张图片发布到新生成的目录中。有人能帮我吗?我已经将问题缩小到它没有move_uploaded_files的事实......

代码:

<?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>

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

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

1 个答案:

答案 0 :(得分:0)

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

应该是

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