[英]upload files, create two new directorys and place the file there PHP
When a user submits the form they enter a ref number and can upload up to 3 documents. 用户提交表单时,他们输入参考编号,最多可以上传3个文档。
When they submit, i want the document to be saved in the folder structure like so: 当他们提交时,我希望将文档保存在文件夹结构中,如下所示:
docs/
12345/1/file.jpg
12345/2/file.jpg
anotherfile.jpg
27635/1/afile.png
anotherfile.png
thirdfile.jpg
34827/1/onefile.jpg
Okay so you get the idea, when a user uploads a file, it makes a new folder inside docs/ with their reference number then makes another folder called 1/with the files. 好的,这样您就可以理解,当用户上传文件时,它会在docs /中使用参考编号创建一个新文件夹,然后使用文件创建一个名为1 /的文件夹。 if the user then uploads again with the same reference it will create a folder inside their reference with the next number up containing their files. 如果用户随后再次使用相同的参考文献进行上传,则会在其参考文献中创建一个文件夹,其中下一个数字将包含其文件。
HTML: 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:
<?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");
}
?>
When i try all i get is the error: Warning: mkdir(): No such file or directory in C:\\wamp\\www\\test\\upload.php on line 8 当我尝试全部操作时,出现以下错误:警告: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(docs / 123/1 / 1.png):无法打开流:第32行的C:\\ wamp \\ www \\ test \\ upload.php中没有此类文件或目录
Warning: move_uploaded_file(): Unable to move 'C:\\wamp\\tmp\\php2E4E.tmp' to 'docs/123/1/1.png' in C:\\wamp\\www\\test\\upload.php on line 32 警告:move_uploaded_file():无法在第32行的C:\\ wamp \\ www \\ test \\ upload.php中将'C:\\ wamp \\ tmp \\ php2E4E.tmp'移动到'docs / 123/1 / 1.png'
Any ideas on this? 有什么想法吗? i'm just scratching my head over this one 我只是在这头上挠头
I see 2 problems. 我看到两个问题。
First, as you are working with windows you need to use \\ instead of / as directory separator. 首先,在使用Windows时,您需要使用\\代替/作为目录分隔符。 Best if you use the php constant DIRECTORY_SEPARATOR
. 最好是使用php常量DIRECTORY_SEPARATOR
。
Second, in order to recursively create directories, you need the third parameter of mkdir like so 其次,为了递归创建目录,您需要像这样的mkdir的第三个参数
mkdir($mypath,0777,TRUE);
Putting that together you should get something like this: 将它们放在一起,您将得到如下内容:
$target_dir = "docs".DIRECTORY_SEPARATOR;
$ref = $_POST['reference'];
if(!file_exists($target_dir . $ref . DIRECTORY_SEPARATOR)){
mkdir($target_dir . $ref . DIRECTORY_SEPARATOR . "1" . DIRECTORY_SEPARATOR, 0777, true);
$count = 0;
}else{
//count the amount of folders inside docs/$ref/
$find_folders = glob($target_dir . $ref . DIRECTORY_SEPARATOR . "*",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 . DIRECTORY_SEPARATOR . $new_folder . DIRECTORY_SEPARATOR, 0777, true);
}
//If count exists then the $target_file changes to the new folder
if($count > 0){
$target_file = $target_dir . $ref . DIRECTORY_SEPARATOR . $new_folder . DIRECTORY_SEPARATOR;
}else{//else use first directory
$target_file = $target_dir . $ref . DIRECTORY_SEPARATOR . "1" . DIRECTORY_SEPARATOR;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.