上传文件,创建两个新目录并将文件放在PHP中


upload files, create two new directorys and place the file there PHP

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

警告:move_uploaded_file():无法将 C:''wamp''tmp''php2E4E.tmp' 移动到第 32 行 C:''wamp''www''test''upload.php 中的 'docs/123/1/1.png'

对此有什么想法吗? 我只是在挠头

我看到 2 个问题。

首先,当您使用窗口时,您需要使用 '' 而不是/作为目录分隔符。最好使用 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;
    }