|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-05-25 18:02 UTC] cbelin at free dot fr
Description:
------------
Recursive 'mkdir' (with PHP 5.0.4 and Windows) doesn't function correctly when the path includes a non-existent root folder.
Reproduce code:
---------------
<?php
mkdir('\toto\tata\titi', 0777, true);
?>
Expected result:
----------------
The following structure of directories should have been created :
\toto\tata\titi
Actual result:
--------------
No directory being created ! The function 'mkdir' fails if directory '\toto' doesn't already exists.
Instead, it outputs :
Warning: mkdir() [function.mkdir]: No such file or directory in xxx.php on line xxx
So I must use this code :
<?php
mkdir('\toto');
mkdir('\toto\tata\titi', 0777, true);
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 21:00:02 2025 UTC |
Note : this bug doesn't appear when you specify a drive letter. <?php mkdir('C:\toto\tata\titi', 0777, true); ?>When the top directory doesn't exist, the code tries to create directory with empty name because on the previous step we changed first "/" to '\0'. IMO the simplest way is to check if the first character is 0 and change it to DEFAULT_SLASH. Not sure how it would work on Windows though. Index: main/streams/plain_wrapper.c =================================================================== RCS file: /repository/php-src/main/streams/plain_wrapper.c,v retrieving revision 1.47 diff -u -p -d -r1.47 plain_wrapper.c --- main/streams/plain_wrapper.c 20 Jun 2005 15:59:12 -0000 1.47 +++ main/streams/plain_wrapper.c 23 Jun 2005 20:06:47 -0000 @@ -1091,6 +1091,9 @@ static int php_plain_files_mkdir(php_str break; } } + if (*buf == 0) { + *buf = DEFAULT_SLASH; + } if (p == buf) { ret = php_mkdir(dir, mode TSRMLS_CC); } else if (!(ret = php_mkdir(buf, mode TSRMLS_CC))) {I have made a little test and I figure out taht giving the full path is not required but that you have to use the backslashes instead of the slashes to make the recursive mkdir work : <code> mkdir ('toto/tutu/titi', 0777, true); # FAILS mkdir ('toto\tutu\titi', 0777, true); # WORKS </code> But when not in recursive mode, mkdir works with either slashes or backslashes.I see this bug on Mac OS X 10.4.5 with PHP 5.1.2. I've also noticed what I assume is a related problem with creating directories in the current directory. For example, if I run the script below in /tmp, the second (and fifth) mkdir will fail. <?php // e.g. running in /tmp mkdir('new1', 0700, true); // works mkdir('new2/sub', 0700, true); // fails mkdir('/tmp/new3/sub', 0700, true); // works mkdir('../tmp/new4/sub', 0700, true); // works mkdir('/new5/sub', 0700, true); // fails, confirming // the original bug. ?>It still does not work with '/' and recursive mode under windows : <code> <?php mkdir ('toto/tutu/titi', 0777, true ); ?> </code> Warning: mkdir(): No such file or directory in C:\var\downloads\php5.2-win32-latest\test_mkdir.php on line 2 and <code> <?php mkdir ('C:/var/downloads/php5.2-win32-latest/toto/tutu/titi', 0777, true ); ?> </code> Using '\' works : <code> <?php mkdir ('toto\tutu\titi', 0777, true ); ?> </code> and <code> <?php mkdir ('C:\var\downloads\php5.2-win32-latest\toto\tutu\titi', 0777, true ); ?> </code> work fine. There is no problem in non-recursive mode and the following works fine : <code> $path = 'tata/toto/tutu/titi'; $parts = explode( '/', $path ); $trail = ''; foreach( $parts as $part ) { $trail .= $part . '/'; if ( file_exists( $trail ) ) { return false; } else { if ( is_dir( dirname( $trail ) ) ) { mkdir ( $trail, 0777 ); } } } </code>