上传文件,创建两个新的directorys并将文件放在那里PHP

时间:2015-01-15 14:09:32

标签: php forms file-upload

当用户提交表单时,他们输入参考编号并最多可以上传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):无法打开流:第32行的C:\ wamp \ www \ test \ upload.php中没有此类文件或目录

警告:move_uploaded_file():无法移动&#39; C:\ wamp \ tmp \ php2E4E.tmp&#39;到&#39; docs / 123/1 / 1.png&#39;在第32行的C:\ wamp \ www \ test \ upload.php

有关于此的任何想法吗?我只是抓住这一个

1 个答案:

答案 0 :(得分:1)

我看到了2个问题。

首先,当您使用Windows时,您需要使用\而不是/作为目录分隔符。最好使用php常量DIRECTORY_SEPARATOR

其次,为了递归创建目录,你需要mkdir的第三个参数,如此

mkdir($mypath,0777,TRUE);

把它放在一起你应该得到这样的东西:

$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;
    }