Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 675
NDM Avatar asked Sep 09 '09 11:09

NDM


2 Answers

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
like image 195
Joseph Lust Avatar answered Sep 23 '22 11:09

Joseph Lust


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

umask(0777);
mkdir($path, 0777, true);
like image 24
Faruk Omar Avatar answered Sep 22 '22 11:09

Faruk Omar