尝试验证使用PHP动态创建的文件夹

时间:2013-07-24 15:58:58

标签: php validation directory

我正在尝试验证:我尝试创建的文件夹是否存在。当然,我收到一条消息......一条错误信息:/告诉我它! (如果它不存在,没有错误信息,一切都按计划进行)。

它输出此错误消息

Directory exists

Warning: mkdir() [function.mkdir]: File exists in /home/***/public_html/***/formulaires/processForm-test.php on line 75

UPLOADS folder has NOT been created

我使用的当前代码就是这个:

$dirPath = $_POST['company'];  

if(is_dir($dirPath))
  echo 'Directory exists';
else
  echo 'directory not exist';

function ftp_directory_exists($ftp, $dirPath) 
{ 
    // Get the current working directory 
    $origin = ftp_pwd($ftp); 

    // Attempt to change directory, suppress errors 
    if (@ftp_chdir($ftp, $dirPath)) 
    { 
        // If the directory exists, set back to origin 
        ftp_chdir($ftp, $origin);    
        return true; 
    } 

    // Directory does not exist 
    return false; 
} 


$result = mkdir($dirPath, 0755);  
if ($result == 1) {  
    echo '<br/>'.$dirPath . " has been created".'<br/>';
} else {  
    echo '<br/>'.$dirPath . " has NOT been created".'<br/>';
}

我最近添加了中间部分(我不知道这是否会产生影响)。以“function ftp_directory_exists($ ftp,$ dirPath)”开头的那个“

2 个答案:

答案 0 :(得分:1)

使用file_exists()检查文件/目录是否存在:

if(!file_exists('/path/to/your/directory')){

//yay, the directory doesn't exist, continue

}

答案 1 :(得分:1)

您添加的函数ftp_directory_exists不会对您的代码产生任何影响,因为它永远不会被调用...

您可能会尝试这样的事情(未经过测试......):

$dirPath = $_POST['company'];  
$dirExists = is_dir($dirPath);

if(!dirExists)
    $dirExists = mkdir($dirPath, 0755);  

echo '<br/>'.$dirPath . (($dirExists)? "" : "DO NOT") . " exists".'<br/>';