opendir() - 打开目录句柄 - php 文件目录函数
opendir()
(PHP 4, PHP 5, PHP 7)
打开目录句柄
说明
opendir(string $path[,resource $context]): resource打开一个目录句柄,可用于之后的closedir(),readdir()和rewinddir()调用中。
参数
$path要打开的目录路径
$context$context参数的说明见手册中的Streams API一章。
返回值
如果成功则返回目录句柄的resource,失败则返回FALSE
。
如果$path不是一个合法的目录或者因为权限限制或文件系统错误而不能打开目录,opendir()返回FALSE
并产生一个E_WARNING级别的 PHP 错误信息。可以在opendir()前面加上“@”符号来抑制错误信息的输出。
更新日志
版本 | 说明 |
---|---|
5.0.0 | $path支持ftp://URL wrapper |
4.3.0 | $path可以是任何支持目录列表的 URL,不过在 PHP 4 中只有file://URL wrapper 支持此功能 |
范例
Example #1opendir()例子
以上例程的输出类似于:
filename: . : filetype: dir filename: .. : filetype: dir filename: apache : filetype: dir filename: cgi : filetype: dir filename: cli : filetype: dir
参见
is_dir()
判断给定文件名是否是一个目录readdir()
从目录句柄中读取条目dir()
返回一个 Directory 类实例
Sometimes the programmer needs to access folder content which has arabic name but the opendir function will return null resources id for that we must convert the dirname charset from utf-8 to windows-1256 by the iconv function just if the preg_match function detect arabic characters and use " U " additionality to enable multibyte matching
iterated function that searches a folder or file in a directory.
Here is a snippet to create a site map of all html files in a folder:
Here are two versions of the same function to list all files in a directory tree. The first one is recursive (calls itself while going through subdirectories) : The second one is iterative (uses less memory) : The iterative version should be a little faster most of the time, but the big difference is in the memory usage. Here is also a profiling function (works in php5 only) :
A couple of notes on Matt's posts on Windows Network Drives: Since the system() command writes the output of the executed shell command straight to the output buffer, if you wish to hide the return of the mapping command (i.e. "The command completed succesfully" or an error message) from a web browser, you need to alter the command that is sent to the shell so that the output of that command is hidden. You probably thinking "why not just use exec()?", and it's a reasonable question, but for some reason it doesn't always work - I guess it's another NT user permissions issue. If you want to guarantee you app will work with no messing around on the host system, use the system() command. In the Windows command shell, you can hide the output of a command by sending both the output (1) and error (2) messages to "nul" using pipes, in other words ">nul 2>&1" on the end of the command. The username and password order in the "net use..." command needs switching in Matt's post. Here (http://networkm.co.uk/static/drive.html) is a function I wrote to dynamically choose which drive letter to use, based on what is currently mapped and accessible to PHP.
I sometimes find this useful. Hope you will too.
This function sorts files by name as strings, but without regard to case. It also does some handy string formatting of the file size information.
A simple piece to open a directory and display any files with a given extension. Great for things like newsletters, score sheets or the like where you just want to make it easy on the user - they just dump in the file with the correct extension and it's done. A link is given to the file which opens up in a new window.
An other way to recursively walk a directory and it's content, applying a callback to each file. Exemple: Update the last modification time of each file in a folder
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)