PHP opendir() 仅列出文件夹
我想使用 opendir() 仅列出特定文件夹(即 /www/site/)中的文件夹。我想从列表中排除文件以及“.”处的文件。和 '..' 文件夹出现在 Linux 文件夹列表中。我该怎么做呢?
I would like to use opendir() to list only the folders in a particular folder (i.e. /www/site/). I would like to exclude files from the list as well at the '.' and '..' folders that appear on a linux folder listing. How would I go about doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
查看 readdir() 的 PHP 文档。它包含一个具体的示例。
为了完整起见:
只需将
opendir('.')
更改为您的目录,即opendir('/www/sites/')
,然后更新$blacklist 包含您不希望输出的文件或目录的名称。
Check out the PHP docs for readdir(). It includes an example for exactly this.
For completeness:
Simply change
opendir('.')
to your directory, i.e.opendir('/www/sites/')
, and update$blacklist
to include the names of files or directory you do not wish to output.您可以简单地使用 glob 和 GLOB_ONLYDIR ,然后过滤结果目录
You can use simply glob with GLOB_ONLYDIR and then filter resulted directories
该函数返回一个可以迭代或存储在某处的数组,这是 99.37% 使用
opendir
的程序员想要的。This function returns an array you can iterate over or store somewhere, which is what 99.37% of all programmers using
opendir
want.仅列出文件夹(目录):
List only folders (Directories):
使用
glob('*')
函数尝试上面的代码对我来说适用于列出当前目录中的文件夹,并且我进一步开发了代码以在同一浏览器的新选项卡中打开每个文件夹。这仅显示目录。
Try this with
glob('*')
functionAbove code worked for me for list out folders in current directory and I further developed code to open each folder in a new tab in same browser. This is shows only directories.
还可以在表单中使用来创建文件夹名称的下拉列表(此处为图像文件夹)。确保上传图像的用户将其推送到正确的文件夹:-)
Can also be used in forms to create a Dropdown of Folder names (here it is the images folder). Ensures that a user uploading an image pushes it to the correct folder :-)