检查PHP中是否存在一个目录

时间:2022-03-30 10:29:13

I know, I know, this sounds soo easy. But I can't seem to find the correct answer on the Internet.

我知道,我知道,这听起来很容易。但是我似乎在网上找不到正确的答案。

One of the solution I found was to use is_dir.

我发现的一个解决方案是使用is_dir。

if(is_dir($dir))
  echo 'directory exists';
else
  echo 'drectory not exist';

But this is wrong-- All this function does is to check whether the $dir is a directory, it doesn't check whether the directory exists, or not. In other words if I put:

但是这是错误的——这个函数所做的就是检查$dir是否是一个目录,它不检查该目录是否存在。换句话说,如果我说

$rootDir = "C:\\Documents and Settings\\test\\My Documents\\Image Directory\\Me Dog\\";

then the function will return a true, even though you can find no such directory on your web server.

然后该函数将返回一个true,即使您在web服务器上找不到这样的目录。

Any ideas?

什么好主意吗?

4 个解决方案

#1


54  

Should work correctly. From is_dir() documentation:

应该正常工作。文档的作用是:判断给定文件名是否是:

Returns TRUE if the filename exists and is a directory, FALSE otherwise.

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

Well, anyway if it doesn't try this:

好吧,不管怎样,如果它不尝试这个:

if(file_exists($dir) && is_dir($dir))

BTW. results of these functions are cached in stat cache. Use clearstatcache() to clean that cache.

顺便说一句。这些函数的结果缓存在stat缓存中。使用clearstatcache()清理缓存。

#2


3  

You'll probably want to use opendir() after is_dir() agrees that the path (could) be a directory.

在is_dir()同意路径(可能)是一个目录之后,您可能希望使用opendir()。

If the resource returned by opendir() is valid, you know you have a directory, and already have a handle to read it.

如果opendir()返回的资源是有效的,则您知道您有一个目录,并且已经有一个读取它的句柄。

Just be sure to call closedir(), either way, if a valid handle is returned.

只要确保在返回有效句柄时调用closedir()即可。

Edit:

编辑:

This answer assumes that you'll be opening the directory either way. If you just need to ensure a path is sane / valid, file_exists() is much cheaper.

这个答案假设您将以任何一种方式打开这个目录。如果只需要确保路径是健全/有效的,file_exists()要便宜得多。

#3


1  

You could try this:

你可以试试这个:

if(is_dir($dir) && is_writeable($dir))
{
    // ...
}

#4


1  

bool file_exists ( string $filename )

Checks whether a file or directory exists.

检查文件或目录是否存在。

http://php.net/manual/en/function.file-exists.php

http://php.net/manual/en/function.file-exists.php

#1


54  

Should work correctly. From is_dir() documentation:

应该正常工作。文档的作用是:判断给定文件名是否是:

Returns TRUE if the filename exists and is a directory, FALSE otherwise.

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

Well, anyway if it doesn't try this:

好吧,不管怎样,如果它不尝试这个:

if(file_exists($dir) && is_dir($dir))

BTW. results of these functions are cached in stat cache. Use clearstatcache() to clean that cache.

顺便说一句。这些函数的结果缓存在stat缓存中。使用clearstatcache()清理缓存。

#2


3  

You'll probably want to use opendir() after is_dir() agrees that the path (could) be a directory.

在is_dir()同意路径(可能)是一个目录之后,您可能希望使用opendir()。

If the resource returned by opendir() is valid, you know you have a directory, and already have a handle to read it.

如果opendir()返回的资源是有效的,则您知道您有一个目录,并且已经有一个读取它的句柄。

Just be sure to call closedir(), either way, if a valid handle is returned.

只要确保在返回有效句柄时调用closedir()即可。

Edit:

编辑:

This answer assumes that you'll be opening the directory either way. If you just need to ensure a path is sane / valid, file_exists() is much cheaper.

这个答案假设您将以任何一种方式打开这个目录。如果只需要确保路径是健全/有效的,file_exists()要便宜得多。

#3


1  

You could try this:

你可以试试这个:

if(is_dir($dir) && is_writeable($dir))
{
    // ...
}

#4


1  

bool file_exists ( string $filename )

Checks whether a file or directory exists.

检查文件或目录是否存在。

http://php.net/manual/en/function.file-exists.php

http://php.net/manual/en/function.file-exists.php