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

使用is_dir函数就足够了吗?

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

或者我应该结合is_dir与file_exists?

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

当前回答

我认为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

其他回答

第二个变量在问题post是不行的,因为,如果你已经有同名的文件,但它不是一个目录,!file_exists($dir)将返回false,文件夹将不会被创建,所以错误“failed to open stream: No such file or directory”将会发生。在Windows中,'文件'和'文件夹'类型是不同的,因此需要同时使用file_exists()和is_dir(),例如:

if (file_exists('file')) {
    if (!is_dir('file')) { //if file is already present, but it's not a dir
        //do something with file - delete, rename, etc.
        unlink('file'); //for example
        mkdir('file', NEEDED_ACCESS_LEVEL);
    }
} else { //no file exists with this name
    mkdir('file', NEEDED_ACCESS_LEVEL);
}

检查路径是否为directory的方法如下:

function isDirectory($path) {
    $all = @scandir($path);
    return $all !== false;
}

注意:对于不存在的路径,它也会返回false,但是对于UNIX/Windows非常有效

我也有同样的疑问,但请参阅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。

我认为这是dir检查的快速解决方案。

$path = realpath($Newfolder);
if (!empty($path)){
   echo "1";
}else{
   echo "0";
}

我就是这么做的

if(is_dir("./folder/test"))
{
  echo "Exist";
}else{
  echo "Not exist";
}