PHP mkdir( $recursive = true ) 跳过最后一个目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1399174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:24:14  来源:igfitidea点击:

PHP mkdir( $recursive = true ) skips last directory

phprecursionpathfilesystemsmkdir

提问by NDM

I've got the following piece of code on a PHP 5.2.4 (no safe_mode) linux server:

我在 PHP 5.2.4 (no safe_mode) linux 服务器上有以下代码:

mkdir( $path, 0777, true );

when I enter a path like:

当我输入如下路径时:

'/path/to/create/recur/ively/'

all directories are created except for the last one... when I add another directory like:

除了最后一个目录外,所有目录都被创建......当我添加另一个目录时:

'/path/to/create/recur/ively/more/'

again, all paths are created except for the last one...

再次,除了最后一个路径之外,所有路径都被创建......

have tried both with and without trailing slashes

试过有和没有斜杠

Can any1 enlighten me here please?

any1可以在这里启发我吗?

回答by NDM

Ok the solutions is as follows: there was no problem.

好的解决方案如下:没有问题。

I did not test the code in isolation, but only assumed the following code was not doing anything to the directory structure...

我没有单独测试代码,只是假设下面的代码没有对目录结构做任何事情......

as I found out the directory got deleted later on by the code itself.

因为我发现该目录后来被代码本身删除了。

Anyway, Lesson learned...

无论如何,教训...

回答by André Hoffmann

Try to remove the trailing slash from your path.

尝试从您的路径中删除尾部斜杠。

At least that's how it's being used in the examples of the mkdirdocumentation.

至少这就是它在mkdir文档示例中的使用方式。

Personally I can't remember having problems, but I usally don't append trailing slashes, so go and try that.

我个人不记得有问题,但我通常不附加斜杠,所以去试试吧。

UPDATE:

更新

I just tried your code and it created every directory including the last one. I'm running Mac OS X 10.5. No idea why it's not working for you :-(

我刚刚尝试了你的代码,它创建了每个目录,包括最后一个。我正在运行 Mac OS X 10.5。不知道为什么它不适合你:-(

That's the code I used:

这是我使用的代码:

<?php
$path = '/Users/andre/test/bla/foo';
mkdir( $path, 0777, true );

Sorry, seems like I'm of no help here.

抱歉,好像我在这里没有帮助。

回答by w35l3y

If you tried everything and it keeps not working, then add some text in the end of the path like:

如果您尝试了所有方法但仍然无法正常工作,请在路径末尾添加一些文本,例如:

$path = '/path/to/create/recur/ively/more/this_wont_be_created_anyway';

回答by Anti Veeranna

What is your PHP version? Is safe_mode turned on?

你的PHP版本是什么?安全模式是否开启?

If so, then it could be that you are experiencing http://bugs.php.net/bug.php?id=43276

如果是这样,那么您可能遇到了http://bugs.php.net/bug.php?id=43276

回答by Faruk Omar

The intermediate directories created are set based on the current umask. You want something like this

创建的中间目录是根据当前的 umask 设置的。你想要这样的东西

umask(0777);
mkdir($path, 0777, true);

回答by Ajay Patidar

Function that create all directories (folders) of given path. No need to write code create each directories (folders) of given path. it will create all directories (folders).

创建给定路径的所有目录(文件夹)的函数。无需编写代码创建给定路径的每个目录(文件夹)。它将创建所有目录(文件夹)。

Like : If you want to create directory structure like
organizations / 1 / users / 1 /

喜欢:如果你想创建像组织/1/用户/1/这样的目录结构

So you only need to call this function with directories path like
$directories_path = 'organizations/1/users/1/';
createUploadDirectories($directories_path);

所以你只需要用目录路径调用这个函数,比如
$directories_path = 'organizations/1/users/1/';
createUploadDirectories($directories_path);

/*
* Method Name : createUploadDirectories
* Parameter : null
* Task : Loading view for create directries for upload
*/

if ( ! function_exists('createUploadDirectories')){
    function createUploadDirectories($upload_path=null){
        if($upload_path==null) return false;
        $upload_directories = explode('/',$upload_path);
        $createDirectory = array();
        foreach ($upload_directories as $upload_directory){
            $createDirectory[] = $upload_directory;
            $createDirectoryPath = implode('/',$createDirectory);
            if(!is_dir($createDirectoryPath)){
                $old = umask(0); 
                mkdir($createDirectoryPath,DIR_WRITE_MODE);// Create the folde if not exist and give permission
                umask($old); 
            }               
        }
        return true;
    }
}

回答by Joseph Lust

You'll get this error if you make the silly mistake I did and pass a string, rather than the numeric literal for mode.

如果你犯了我犯的愚蠢错误并传递了一个字符串,而不是模式的数字文字,你就会得到这个错误。

mkdir( $path, "0777", true ); // BAD - only creates /a/b

mkdir( $path, 0777, true ); // GOOD - creates /a/b/c/d