【发布时间】:2015-01-15 14:09:32
【问题描述】:
当用户提交表单时,他们输入一个参考号,最多可以上传 3 个文档。
当他们提交时,我希望将文档保存在文件夹结构中,如下所示:
docs/
12345/1/file.jpg
12345/2/file.jpg
anotherfile.jpg
27635/1/afile.png
anotherfile.png
thirdfile.jpg
34827/1/onefile.jpg
好的,你明白了,当用户上传文件时,它会在 docs/ 中创建一个新文件夹,其中包含他们的参考号,然后创建另一个名为 1/ 的文件夹,其中包含文件。如果用户随后使用相同的引用再次上传,它将在他们的引用中创建一个文件夹,下一个编号包含他们的文件。
HTML:
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="text" name="reference"/><br/>
<input type="file" name="pictures[]" /><br/>
<input type="file" name="pictures[]" /><br/>
<input type="file" name="pictures[]" /><br/>
<input type="submit" value="Send" />
</form>
PHP:
<?php
$target_dir = "docs/";
$ref = $_POST['reference'];
if(!file_exists($target_dir . $ref . '/')){
mkdir($target_dir . $ref . "/1/");
$count = 0;
}else{
//count the amount of folders inside docs/$ref/
$find_folders = glob($target_dir . $ref . "/*",GLOB_ONLYDIR);
$count = count($find_folders);
//create new folder inside $ref/ using count+1 to make the folder increase by 1
$new_folder = $count +1;
mkdir($target_dir . $ref . "/" . $new_folder . "/");
}
//If count exists then the $target_file changes to the new folder
if($count > 0){
$target_file = $target_dir . $ref . $new_folder . "/";
}else{//else use first directory
$target_file = $target_dir . $ref ."/1/";
}
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");
}
?>
当我尝试时,我得到的只是错误: 警告:mkdir(): 第 8 行的 C:\wamp\www\test\upload.php 中没有这样的文件或目录
arning: move_uploaded_file(docs/123/1/1.png): failed to open stream: No such file or directory in C:\wamp\www\test\upload.php on line 32
警告:move_uploaded_file():无法将“C:\wamp\tmp\php2E4E.tmp”移动到 C:\wamp\www\test\upload.php 中的“docs/123/1/1.png”第 32 行
对此有什么想法吗?我只是在这个问题上摸不着头脑
【问题讨论】:
-
确保
$target_dir确实将您带到目录...因为看起来没有。 -
为什么不呢?我确定是这样吗?
-
1.上面这个 PHP 文件的路径是什么? 2. 现在
docs的路径是什么? -
主目录是test,里面是我的index.php和表单,upload.php是表单提交和docs/文件夹
-
文件夹 docs/123/1/ 是否已创建?
标签: php forms file-upload