如何通过PHP检查目录是否存在?

Pet*_*ter 306 php

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

是否is_dir足够用于此目的?

if ( !is_dir( $dir ) ) {
    mkdir( $dir );       
}
Run Code Online (Sandbox Code Playgroud)

或者我应该结合is_dir使用file_exists

if ( !file_exists( $dir ) && !is_dir( $dir ) ) {
    mkdir( $dir );       
} 
Run Code Online (Sandbox Code Playgroud)

Mar*_*c B 204

两者都会在Unix系统上返回true - 在Unix中,一切都是文件,包括目录.但要测试是否采用该名称,您应该检查两者.可能有一个名为'foo'的常规文件,这会阻止您创建目录名'foo'.

  • 别忘了检查是否也是可写的 (33认同)
  • @Drewdin你想检查`is_writable`的父母不是吗? (8认同)

小智 125

$dirname = $_POST["search"];
$filename = "/folder/" . $dirname . "/";

if (!file_exists($filename)) {
    mkdir("folder/" . $dirname, 0777);
    echo "The directory $dirname was successfully created.";
    exit;
} else {
    echo "The directory $dirname exists.";
}
Run Code Online (Sandbox Code Playgroud)

  • 回声说的很多...... (45认同)
  • 考虑到它需要后期输入并按原样使用,加上0777目录,根本不是那么安全; P (13认同)
  • 当你使用`mkdir`时 - 你为什么不传递'$ filename'? (7认同)
  • 更严重的是, $dirname 可以被清理,并且权限可以设置为 0755。添加一些 .htaccess 指令。OWASP 还有一些进一步的建议:https://www.owasp.org/ (2认同)

inc*_*kie 16

我认为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;
}
Run Code Online (Sandbox Code Playgroud)

相同功能的短版本

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

输出示例

<?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) "/"
Run Code Online (Sandbox Code Playgroud)

用法

<?php

$folder = '/foo/bar';

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

mkdir($folder);
// Continue do stuff
Run Code Online (Sandbox Code Playgroud)

  • 对于遇到此问题的任何人,我相信realpath会在文件夹运行时缓存它们,因此如果它运行一次,那么在此之后删除文件夹,如果再次运行它可能不会返回false. (2认同)
  • file_exists也是如此 (2认同)

Boo*_*ype 7

问题帖子中的第二个变种是不行的,因为,如果你已经拥有相同名称的文件,但它不是目录,!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);
}
Run Code Online (Sandbox Code Playgroud)


小智 5

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