is_dir() - 判断给定文件名是否是一个目录 - php 文件目录函数
is_dir()
(PHP 4, PHP 5, PHP 7)
判断给定文件名是否是一个目录
说明
is_dir(string $filename): bool判断给定文件名是否是一个目录。
参数
$filename如果文件名存在并且为目录则返回TRUE
。如果$filename是一个相对路径,则按照当前工作目录检查其相对路径。If$filenameis a symbolic or hard link then the link will be resolved and checked. If you have enabled安全模式,oropen_basedirfurther restrictions may apply.
返回值
如果文件名存在,并且是个目录,返回TRUE
,否则返回FALSE
。
范例
Example #1is_dir()例子
以上例程会输出:
bool(false) bool(false) bool(true)
错误/异常
失败时抛出E_WARNING
警告。
注释
Note:此函数的结果会被缓存。参见clearstatcache()以获得更多细节。
Tip自 PHP 5.0.0 起,此函数也用于某些URL 包装器。请参见支持的协议和封装协议以获得支持stat()系列函数功能的包装器列表。
参见
chdir()
改变目录dir()
返回一个 Directory 类实例opendir()
打开目录句柄is_file()
判断给定文件名是否为一个正常的文件is_link()
判断给定文件名是否为一个符号连接
Just a note for anyone who encounters is_dir() returning false on CIFS mount points or directories within those mount points on 2.6.31 and newer kernels: Apparently in new kernels they've started using the CIFS serverino option by default. With Windows shares this causes huge inode numbers and which apparently can cause is_dir() to return false. Adding the noserverino option to the CIFS mount will prevent this. This may only occur on 32 systems but I don't have a 64 bit install to test against.
Note that on Linux is_dir returns FALSE if a parent directory does not have +x (executable) set for the php process.
This is the "is_dir" function I use to solve the problems : function Another_is_dir ($file) { if ((fileperms("$file") & 0x4000) == 0x4000) return TRUE; else return FALSE; } or, more simple : function Another_is_dir ($file) { return ((fileperms("$file") & 0x4000) == 0x4000); } I can't remember where it comes from, but it works fine.
Note that this functions follows symbolic links. It will return true if the file is actually a symlink that points to a directory. An example: Prints out: bool(true) (Windows Note: Under recent versions of Windows you can set symlinks as long as you're administrator, but you cannot remove directory symlinks with "unlink()", you will have to use "rmdir testlink" from the shell to get rid of it.)
My solution to the problem that you must include the full path to make "is_dir" work properly as a complete example:
One note regarding checking for empty directories : >>echo (count(glob("$dir/*")) === 0) ? 'Empty' : 'Not empty'; This does not work correctly on Linux. The '.' and '..' will always be returned even if no files are present in the directory.
Running PHP 5.2.0 on Apache Windows, I had a problem (likely the same one as described by others) where is_dir returned a False for directories with certain permissions even though they were accessible. Strangely, I was able to overcome the problem with a more complete path. For example, this only displays "Works" on subdirectories with particular permissions (in this directory about 1 out of 3): $d = opendir("./albums/mydir"); while(false !== ($f = readdir($d))) { echo "
"; if(is_dir($f)) { echo "Works:" . $f . ""; } } However, this works properly for all directories: $d = opendir("./albums/mydir"); while(false !== ($f = readdir($d))) { echo "
"; $dName = "./albums/mydir/" . $f; if(is_dir($dName)) { echo "Works:" . $dName . ""; } } I don't understand the hit-and-miss of the first code, but maybe the second code can help others having this problem.
When trying (no 'pear') to enumerate mounted drives on a win32 platform (Win XP SP3, Apache/2.2.11, PHP/5.2.9), I used: which yielded: A: C: D: E: F: G: H: I:
PITFALL in sub dir processing After struggeling with a sub-dir processing (some subdirs were skipped) AND reading the posts, I realized that virutally no-one clearly told what were wrong. The common traverse dir code was: ----------------------------------------- opendir("myphotos"); // Top dir to process from (example) while (false !== ($fname = readdir($h_dir))) { // process current dir (read a directory entry) if ($fname{0} == '.') continue; // skip dirs . and .. by first char test if (is_dir($fname)) call_own_subdir_process; // process this subdir by calling a routine } PROBLEM IS : The "is_dir()" must have the FULL PATH or it will skip some dirs. So the above code need to INSERT THE PATH before the filename. This would give this change in above... if (is_dir("myphotos\" . $fname)) call_own_subdir_process; // skip subdirs The pitfall really was, that without full path some subdirs were found...hope this clears all up
Note that is_dir() also works with ftp://. For example : But note that if the connexion fails due to invalide credentials, this will consider that the folder doesn't exist and will return FALSE.
Note that there quite a few articles on the net that imply that commands like is_dir, opendir, readdir cannot read paths with spaces. On a linux box, THAT is not an issue. Sample test code; $dir = "Images/Soma ALbum Name with spaces"; echo $dir."
"; // Open a directory, and read its contents if (is_dir($dir)){ echo $dir."
"; // will not appear if above fails if ($dh = opendir($dir)){ echo $dir."
"; // will not appear if above fails while (($file = readdir($dh)) !== false){ echo "filename:" . $file . "
"; echo $dir."
"; // will not appear if above fails } closedir($dh); } }
Here is another way to test if a directory is empty, which I think is much simpler than those posted below:
better ;)
Ah ha! Maybe this is a bug, or limitation to be more precise, of php. See http://bugs.php.net/bug.php?id=27792 A workaround is posted on the page (above) and seems to work for me: function is_dir_LFS($path){ return (('d'==substr(exec("ls -dl '$path'"),0,1))?(true):(false)); } PS: I'm using PHP 4.3.10-16, posts report this problem up to 5.0
this function bypasses open_basedir restrictions. example: output: Warning: open_basedir restriction in effect output: true (or false, depending whether it is or not...) --- visit puremango.co.uk for other such wonders
use this function to get all files inside a directory (including subdirectories)
An even better (PHP 5 only) alternative to "Davy Defaud's function": NOTE: you should obviously be checking beforehand if $dir is actually a directory, and that it is readable, as only relying on this you would assume that in both cases you have a non-empty readable directory.
When I run a scandir I always run a simple filter to account for file system artifacts (especially from a simple ftp folder drop) and the "." ".." that shows up in every directory:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)