1

我想知道一个目录是否存在。

如果没有,我想创建目录。

我的代码如下:

$da = getdate();
$dat = $da["year"]."-".$da["mon"]."-".$da["mday"];
$m = md5($url)."xml";
if(is_dir($dat))
{
    chdir($dat);
    $fh = fopen($m, 'w');
    fwrite($fh, $xml); 
    fclose($fh);
    echo "yes";
}
else
{
    mkdir($dat,0777,true); 
    chdir($dat);   
    $fh = fopen($m, 'w');   
    fwrite($fh, $xml);    
    fclose($fh); 
    echo "not";
} 
4

2 回答 2

7

使用is_dir,它检查路径是否存在并且是一个目录然后mkdir

function mkdir_if_not_there($path) {
  if (!is_dir($path)) {
    // Watch out for potential race conditions here
    mkdir($path);
  }
}
于 2010-05-25T12:34:38.553 回答
0

使用 is_dir:

$pathname = "/path/to/dir";
if(is_dir($pathname)) {
   // do something
}
于 2010-05-25T12:36:14.153 回答