找出php中是否存在目录

zah*_*in 1 php mkdir chdir

我想知道目录是否存在.

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

我的代码如下:

$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";
} 
Run Code Online (Sandbox Code Playgroud)

Dom*_*ger 7

使用is_dir,检查路径是否存在,然后是目录mkdir.

function mkdir_if_not_there($path) {
  if (!is_dir($path)) {
    // Watch out for potential race conditions here
    mkdir($path);
  }
}
Run Code Online (Sandbox Code Playgroud)