Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    mkdir problems....can't figure it out...

    I am just trying to create a simple directory through PHP..


    <?php
    if(mkdir("path/to/my/dir", 0777)) {
    echo "dir created.";
    } else {
    die("error");
    }
    ?>

    I get the following error....


    Warning: mkdir() [function.mkdir]: No such file or directory in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\makedir.php on line 2
    error

    Can someone please help me? I cannot figure out why this is happening...

    Thanks

  • #2
    New Coder
    Join Date
    Feb 2009
    Posts
    38
    Thanks
    0
    Thanked 1 Time in 1 Post
    The 'mkdir' function doesn't function correctly on Windows when the pathname contain space(s) on the final. PHP returns a warning, and the directory isn't created.

    The 'mkdir' function doesn't function correctly on Windows when the path
    contains forward slashes. The part of the path with the forward slashes
    doesn't get created.

    mkdir('c:/a/b/c/d', 0775, true);

    You get the error message:
    Warning: mkdir() [function.mkdir]: No such file or directory

    Please use backslashes under Windows or use the constant DIRECTORY_SEPARATOR.

    mkdir('c:\a\b\c\d', 0775, true);
    mkdir('c:'.DIRECTORY_SEPARATOR.'a'.
    DIRECTORY_SEPARATOR.'b'.
    DIRECTORY_SEPARATOR.'c'.
    DIRECTORY_SEPARATOR.'d', 0775, true);

  • #3
    Regular Coder kokjj87's Avatar
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    279
    Thanks
    1
    Thanked 55 Times in 54 Posts
    You need to create the directory recursively, mkdir function does not understand paths..

    Here is how you do it recursively..
    PHP Code:
    <?php
        
    function make_dir($dir$chmod)
        {
            if(!
    is_dir($dir))
            {
                
    $temp explode('/'$dir);
                
    $cur_dir '';
                for(
    $i 0$i count($temp); $i++)
                {
                    
    $cur_dir .= $temp[$i] . '/';
                    if(!
    is_dir($cur_dir))
                    {
                        
    //create the folders
                        
    if(@ mkdir($cur_dir$chmod) && ($cur_dir != getcwd()))
                        {
                            echo  
    $cur_dir." Ok.<br/>";
                        }
                    }
                }
            }
        }
        
        
    make_dir('path/to/my/dir'0777);
    ?>

  • #4
    New Coder
    Join Date
    Jan 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the help guys.... Problem was the forward slashes...changed them to \ and it seems to work fine now..

    thanks again!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •