需要获得一个想法,将pdf文件上传到动态选择的目录

时间:2014-12-30 17:34:15

标签: php html wamp

我想要一种简单的方法将文件上传到目标位置,用户(管理员)可以在上传文件之前将其选中。什么是用于选择服务器中特定目标位置的对话框或此类选择方法。有一个根目录。

在根目录的旁边有一些名为的文件夹 2011,2012,2013,2014。

在每个目录中都有很多目录。

所以我想手动选择一个目录并将文件上传到该目录。我知道用于将文件上传到预定义目录的php代码。现在我想为了我的目的改变代码。

这是我使用的代码: 这是我使用的代码示例:

<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

php代码是

<?php
         $ndir = $_GET['file'];
         echo $ndir;

         $target_path = "uploads/";
         $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
         // Check if file already exists
         if (file_exists($target_path)) {
            echo "Sorry, file already exists.";
           }
        else if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
         echo "The file ".  basename( $_FILES['uploadedfile']['name']). " has been uploaded";
        }
        else{
        echo "There was an error uploading the file, please try again!";
        }
        ?>

由于

1 个答案:

答案 0 :(得分:3)

因为我有几分钟的闲暇时间:

您可以在设置名称属性时使用下拉菜单选项,并将值设置为文件夹名称。

<select name="destination">
   <option value="folder_1">Folder 1</option>
   <option value="folder_2">Folder 2</option>
   <option value="folder_3">Folder 3</option>
</select>

然后将文件夹目标连接到上传/目标路径变量:

if(isset($_POST['destination'])){
  $target_path = "uploads/";
  $target_folder = $_POST['destination'];
  $target_path = $target_path . $target_folder . "/" . basename( $_FILES['uploadedfile']['name'] );
}
  • 事物类型。

修改

这将从给定的起始路径开始扫描整个子文件夹。

Nota:您需要填写上传部分的其余部分。

<form enctype="multipart/form-data" action="" method="POST">

    <select name="destination">

<?php 
/* ============== PATH NOTES ============== */
// Either use full path. Keep the trailing slash
// $yourStartingPath = "/var/user/you/httpdocs/uploads/";

// or use current folder from script's execution
$yourStartingPath = "./";

$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($yourStartingPath, RecursiveDirectoryIterator::SKIP_DOTS),
    RecursiveIteratorIterator::SELF_FIRST,
    RecursiveIteratorIterator::CATCH_GET_CHILD
);

foreach($iterator as $file) {
    if($file->isDir()) {

        echo '<option value="'.$file->getRealPath().'">' . $file->getFilename().'</option>';

    }
}

echo "</select>";

    if(isset($_POST['destination'])){

      $target_folder = $_POST['destination'];
      $target_path = $target_folder . "/" . basename( $_FILES['uploadedfile']['name'] );

    }

echo "<br>";
/* You can comment out the echo $target_path; */
/* It's just for testing purposes to show you the folder */
echo $target_path;
echo "<br>";

?>

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