[英]How do I check if a directory exists? "is_dir", "file_exists" or both?
如果目录不存在,我想创建一个目录。
为此目的使用is_dir
函数是否足够?
if ( !is_dir( $dir ) ) {
mkdir( $dir );
}
或者我应该将is_dir
与file_exists
结合起来吗?
if ( !file_exists( $dir ) && !is_dir( $dir ) ) {
mkdir( $dir );
}
在 Unix 系统上两者都将返回 true - 在 Unix 中,一切都是文件,包括目录。 但是要测试是否使用了该名称,您应该同时检查两者。 可能有一个名为“foo”的常规文件,它会阻止您创建目录名称“foo”。
$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.";
}
我认为 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
帖子中的第二个变体不行,因为如果您已经有同名的文件,但它不是目录, !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);
}
我有同样的疑问,但请参阅 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);
}
$save_folder = "some/path/" . date('dmy');
if (!file_exists($save_folder)) {
mkdir($save_folder, 0777);
}
在0777之后添加true
<?php
$dirname = "small";
$filename = "upload/".$dirname."/";
if (!is_dir($filename )) {
mkdir("upload/" . $dirname, 0777, true);
echo "The directory $dirname was successfully created.";
exit;
} else {
echo "The directory $dirname exists.";
}
?>
这是一个老问题,但仍然是热门问题。 只需使用is_dir()
或file_exists()
函数测试.
或..
被测目录中的文件。 每个目录必须包含以下文件:
is_dir("path_to_directory/.");
好吧,而不是同时检查两者,您可以这样做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(is_dir("./folder/test"))
{
echo "Exist";
}else{
echo "Not exist";
}
检查路径是否为目录的方法如下:
function isDirectory($path) {
$all = @scandir($path);
return $all !== false;
}
注意:对于不存在的路径,它也会返回 false,但对于 UNIX/Windows 非常有效
我认为这是目录检查的快速解决方案。
$path = realpath($Newfolder);
if (!empty($path)){
echo "1";
}else{
echo "0";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.