我想创建一个目录,如果它不存在。

使用is_dir函数就足够了吗?

if ( !is_dir( $dir ) ) {
    mkdir( $dir );       
}

或者我应该结合is_dir与file_exists?

if ( !file_exists( $dir ) && !is_dir( $dir ) ) {
    mkdir( $dir );       
} 

当前回答

好吧,而不是检查两者,你可以这样做if(stream_resolve_include_path($folder)!==false)。它速度较慢,但能一枪打死两只鸟。

另一个选择是忽略E_WARNING,而不是使用@mkdir(…);(因为这将简单地放弃所有可能的警告,不仅仅是目录已经存在一个),而是通过注册一个特定的错误处理程序,然后再这样做:

namespace com\stackoverflow;

set_error_handler(function($errno, $errm) { 
    if (strpos($errm,"exists") === false) throw new \Exception($errm); //or better: create your own FolderCreationException class
});
mkdir($folder);
/* possibly more mkdir instructions, which is when this becomes useful */
restore_error_handler();

其他回答

好吧,而不是检查两者,你可以这样做if(stream_resolve_include_path($folder)!==false)。它速度较慢,但能一枪打死两只鸟。

另一个选择是忽略E_WARNING,而不是使用@mkdir(…);(因为这将简单地放弃所有可能的警告,不仅仅是目录已经存在一个),而是通过注册一个特定的错误处理程序,然后再这样做:

namespace com\stackoverflow;

set_error_handler(function($errno, $errm) { 
    if (strpos($errm,"exists") === false) throw new \Exception($errm); //or better: create your own FolderCreationException class
});
mkdir($folder);
/* possibly more mkdir instructions, which is when this becomes useful */
restore_error_handler();

我也有同样的疑问,但请参阅PHP文档:

https://www.php.net/manual/en/function.file-exists.php

https://www.php.net/manual/en/function.is-dir.php

您将看到is_dir()具有这两个属性。

返回值is_dir 如果文件名存在且为目录,则返回TRUE,否则返回FALSE。

$year = date("Y");   
$month = date("m");   
$filename = "../".$year;   
$filename2 = "../".$year."/".$month;

if(file_exists($filename)){
    if(file_exists($filename2)==false){
        mkdir($filename2,0777);
    }
}else{
    mkdir($filename,0777);
}

这是一个老问题,但仍然是热门话题。只需要使用is_dir()或file_exists()函数测试是否存在。或. .在测试目录中的文件。每个目录必须包含以下文件:

is_dir("path_to_directory/.");    

我认为realpath()可能是验证路径是否存在的最佳方法 http://www.php.net/realpath

下面是一个示例函数:

<?php
/**
 * Checks if a folder exist and return canonicalized absolute pathname (long version)
 * @param string $folder the path being checked.
 * @return mixed returns the canonicalized absolute pathname on success otherwise FALSE is returned
 */
function folder_exist($folder)
{
    // Get canonicalized absolute pathname
    $path = realpath($folder);

    // If it exist, check if it's a directory
    if($path !== false AND is_dir($path))
    {
        // Return canonicalized absolute pathname
        return $path;
    }

    // Path/folder does not exist
    return false;
}

相同函数的简写版本

<?php
/**
 * Checks if a folder exist and return canonicalized absolute pathname (sort version)
 * @param string $folder the path being checked.
 * @return mixed returns the canonicalized absolute pathname on success otherwise FALSE is returned
 */
function folder_exist($folder)
{
    // Get canonicalized absolute pathname
    $path = realpath($folder);

    // If it exist, check if it's a directory
    return ($path !== false AND is_dir($path)) ? $path : false;
}

输出示例

<?php
/** CASE 1 **/
$input = '/some/path/which/does/not/exist';
var_dump($input);               // string(31) "/some/path/which/does/not/exist"
$output = folder_exist($input);
var_dump($output);              // bool(false)

/** CASE 2 **/
$input = '/home';
var_dump($input);
$output = folder_exist($input);         // string(5) "/home"
var_dump($output);              // string(5) "/home"

/** CASE 3 **/
$input = '/home/..';
var_dump($input);               // string(8) "/home/.."
$output = folder_exist($input);
var_dump($output);              // string(1) "/"

使用

<?php

$folder = '/foo/bar';

if(FALSE !== ($path = folder_exist($folder)))
{
    die('Folder ' . $path . ' already exist');
}

mkdir($folder);
// Continue do stuff