我想创建一个目录,如果它不存在.
是否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'.
小智 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)
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)
问题帖子中的第二个变种是不行的,因为,如果你已经拥有相同名称的文件,但它不是目录,!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。
| 归档时间: |
|
| 查看次数: |
347605 次 |
| 最近记录: |