【问题标题】:upload files, create two new directorys and place the file there PHP上传文件,创建两个新目录并将文件放在那里 PHP
【发布时间】: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


【解决方案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;
    }

【讨论】:

  • 非常感谢,mkdir($mypath,0777,TRUE);确实意味着现在当用户第一次尝试时,它会创建正确的路径并将文件放在那里。但是现在,如果用户尝试使用相同的引用再次上传,它会创建文件夹 (2) 但不会在此处输入文件。我得到的错误是:arning: move_uploaded_file(docs/123452/2.png): failed to open stream: No such file or directory in C:\wamp\www\test\upload.php on line 32 and Warning: move_uploaded_file( ): 无法在第 32 行的 C:\wamp\www\test\upload.php 中将“C:\wamp\tmp\php736D.tmp”移动到“docs/123452/2.png”
  • 哦,在 $new_folder 之前这里少了一个 \: $target_file = $target_dir 。 $ 参考。 $新文件夹。 DIRECTORY_SEPARATOR;我会更新我的答案
  • 非常感谢您。一切正常。我知道人们不喜欢这里要求调试的线程。但这确实帮助我掌握了它的工作原理。
  • 顺便说一句;无论您是否正常使用 \ 或 /,Windows 通常都不会提供飞行香肠 - 它甚至对 ./ 之类的东西非常满意 - 您可以使用 realpath() 而不是无数 DIRECTORY_SEPARATORs :php.net/manual/en/function.realpath.php
  • @CD001:不知道斜线在windows下有效,谢谢指正
猜你喜欢
  • 2020-12-21
  • 2021-05-25
  • 2019-03-16
  • 2012-09-23
  • 2014-11-10
  • 1970-01-01
  • 2012-11-24
  • 2021-08-15
  • 1970-01-01
相关资源
最近更新 更多