百科狗-知识改变命运!
--

dir() - 返回一个 Directory 类实例 - php 文件目录函数

是丫丫呀1年前 (2023-11-21)阅读数 16#技术干货
文章标签目录

dir()

(PHP 4, PHP 5, PHP 7)

返回一个 Directory 类实例

说明

dir(string $directory[,resource $context]): Directory

以面向对象的方式访问目录。打开$directory参数指定的目录。

参数

$directory

被打开的目录

$context

Note:在 PHP 5.0.0中增加了对上下文(Context)的支持。有关上下文(Context)的说明参见Streams。

返回值

成功的话,返回一个Directory类实例,参数错误的情况下返回NULL,其它错误情况返回FALSE

范例

Example #1dir()示例

请特别注意下面示例中Directory::read()函数返回值的判断方式。我们严格测试(值相等,并且类型相同,请参考比较运算符)返回值等于FALSE,因为有些情况下,目录名可能"等于"FALSE,导致跳出循环。

以上例程的输出类似于:

Handle: Resource id #2
Path: /etc/php5
.
..
apache
cgi
cli

注释

dir() - 返回一个 Directory 类实例 - php 文件目录函数

Note:

目录条目返回的顺序依赖于系统。

Regarding samuel's comment about the dir() function not supporting Unicode properly, it's all in the encoding. The function does NOT internally change Unicode characters into question marks (?), as I was first led to believe. If you simply try to output them in UTF-8, they'll show up just right.
This one's pretty nice. After getting frustrated for hunting down .jpg files in my massive music collection (PHP would run out of memory), I thought there should be a preg_ls function.
function preg_ls ($path=".", $rec=false, $pat="/.*/") {
  // it's going to be used repeatedly, ensure we compile it for speed.
  $pat=preg_replace("|(/.*/[^S]*)|s", "\\1S", $pat);
  //Remove trailing slashes from path
  while (substr($path,-1,1)=="/") $path=substr($path,0,-1);
  //also, make sure that $path is a directory and repair any screwups
  if (!is_dir($path)) $path=dirname($path);
  //assert either truth or falsehoold of $rec, allow no scalars to mean truth
  if ($rec!==true) $rec=false;
  //get a directory handle
  $d=dir($path);
  //initialise the output array
  $ret=Array();
  //loop, reading until there's no more to read
  while (false!==($e=$d->read())) {
    //Ignore parent- and self-links
    if (($e==".")||($e=="..")) continue;
    //If we're working recursively and it's a directory, grab and merge
    if ($rec && is_dir($path."/".$e)) {
      $ret=array_merge($ret,preg_ls($path."/".$e,$rec,$pat));
      continue;
    }
    //If it don't match, exclude it
    if (!preg_match($pat,$e)) continue;
    //In all other cases, add it to the output array
    $ret[]=$path."/".$e;
  }
  //finally, return the array
  return $ret;
}
Not bad for a mere 18 lines, don't you think?
Example use:
foreach (preg_ls("/etc/X11", true, "/.*\.conf/i") as $file) echo $file."\n";
Output: 
/etc/X11/xkb/README.config
/etc/X11/xorg.conf-vesa
/etc/X11/xorg.conf~
/etc/X11/gui.conf
/etc/X11/xorg.conf
/etc/X11/xorg.conf-fbdev
Here my solution how to do effective recursiv directory listing.
Have fun. 

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)