php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33140 Recursive 'mkdir' doesn't work when path includes a non-existent root folder
Submitted: 2005-05-25 18:02 UTC Modified: 2006-11-15 00:22 UTC
Votes:7
Avg. Score:4.1 ± 0.8
Reproduced:6 of 6 (100.0%)
Same Version:2 (33.3%)
Same OS:3 (50.0%)
From: cbelin at free dot fr Assigned:
Status: Closed Package: Directory function related
PHP Version: 5CVS-2005-05-26 OS: Win32
Private report: No CVE-ID:
 [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);
?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-05-25 19:54 UTC] cbelin at free dot fr
Note :
this bug doesn't appear when you specify a drive letter.

<?php
mkdir('C:\toto\tata\titi', 0777, true);
?>
 [2005-06-23 22:18 UTC] tony2001@php.net
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))) {
 [2005-06-24 04:04 UTC] iliaa@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2005-07-02 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2006-03-15 08:48 UTC] zefredz at gmail dot com
I have the same bug with PHP 5.0.4 and the given snapshot. But other PHP developpers in my development team does not get the error message while using the same PHP version than mine. The problem occurs on my computer with PHP 5.1 too. 

The only difference I can figure out between those developers and I is that I am still using Windows XP SP1 while they are using Windows XP SP2. Can this be the cause of the bug ?
 [2006-03-15 09:02 UTC] zefredz at gmail dot com
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.
 [2006-03-29 20:07 UTC] ksteinhoff at gmail dot com
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.
?>
 [2006-06-20 16:16 UTC] tony2001@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5.2-win32-latest.zip


 [2006-06-28 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2006-07-09 11:14 UTC] zefredz at gmail dot com
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>
 [2006-11-11 22:58 UTC] zefredz at gmail dot com
This issue is fixed under Windows with PHP 5.2.0
 [2006-11-15 00:22 UTC] iliaa@php.net
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at 
http://www.php.net/downloads.php


 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 23 18:01:55 2014 UTC