2012-11-05 39 views
1

以下代码给我错误:“mkdir:file exists”。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); 
     } 
    } 

但是,如果我不放第二条件,它会给我错误:“mkdir:no such file or directory”。另外,如果我通过写入mkdir($ new_location,077,true)添加递归性,我不会收到错误,但目录不会被创建。我只是不明白我在这里可能会做错什么。

+1

'$ path'和'$ new_location'是否有关?如果是这样,怎么样?如果不是,你为什么提到这两个? – Jon

+1

您可以发布$ path和$ new_location的值吗? – Andrea

+1

'mkdir($ new_location,0777,true);'假设'$ new_location'是'$ path。$ some_new_directory' –

回答

1

的错误是由你的路径的双斜杠,其中PHP不喜欢在所有引起的。如果您将c://更改为c:/,它将一切正常。

顺便说一句,没有真正的理由指定0777作为模式,因为这也是默认设置。

0

假定$new_location是当前路径$path,附加了新的目录名称。你将需要mkdir设置为true递归标志,以便它Allows the creation of nested directories specified in the pathname.

http://php.net/manual/en/function.mkdir.php

mkdir($new_location, 0777, true); 
+0

你可以做第二次外逻辑检查 –

+0

好点,去复制/粘贴疯狂... –

0

的错误是自我解释

Warning: mkdir() [function.mkdir]: No such file or directory in 

这意味着父目录不存在..你需要添加递归选项来创建parent directory然后current directory

mkdir($new_location, 0777, true); 

如果你不想这样做,经常检查,如果父目录存在

if (!is_dir(dirname($new_location)) || !is_writable(dirname($new_location))) 
{ 
    trigger_error("Your parent does not exist"); 
}