NDM
NDM

Reputation: 6830

PHP mkdir( $recursive = true ) skips last directory

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

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?

Upvotes: 13

Views: 46154

Answers (7)

Joseph Lust
Joseph Lust

Reputation: 19985

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

Upvotes: 1

Ajay Patidar
Ajay Patidar

Reputation: 314

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 /

So you only need to call this function with directories path like
$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;
    }
}

Upvotes: 0

Faruk Omar
Faruk Omar

Reputation: 1193

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

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

Upvotes: 2

NDM
NDM

Reputation: 6830

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...

Upvotes: 18

Anti Veeranna
Anti Veeranna

Reputation: 11583

What is your PHP version? Is safe_mode turned on?

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

Upvotes: 1

w35l3y
w35l3y

Reputation: 8773

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';

Upvotes: 2

André Hoffmann
André Hoffmann

Reputation: 3555

Try to remove the trailing slash from your path.

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

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 :-(

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.

Upvotes: 8

Related Questions