Search code examples
phpmkdirfile-exists

mkdir, file_exists : confusing and paradoxal errors


The following code gives me the error : "mkdir : file exists".

    $path = 'c://wamp/www/et1/other';
    $new_location = 'c://wamp/www/et1/other/test';
    if(file_exists($path) && is_dir($path))
    {
        if(!file_exists($new_location))
        {               
            mkdir($new_location, 0777);
        }
    }

However, if I don't put the second if condition, it gives me the error : "mkdir : no such file or directory". Also if I add recursivity by writting mkdir($new_location,077,true) I don't get errors but the directory is not created. I just don't understand what I could be doing wrong here.


Solution

  • The error is caused by the double slashes in your paths, which PHP doesn't like at all. If you change c:// to c:/ it will all work just fine.

    And by the way, there's no real reason to specify the 0777 as the mode because that's also the default.