如何获取文件名?
比如,我们有文件夹 /images/
,里面有一些文件。
脚本 /scripts/listing.php
我们如何获取 listing.php
文件夹 /images/
内所有文件的名称代码>?
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
比如,我们有文件夹 /images/
,里面有一些文件。
脚本 /scripts/listing.php
我们如何获取 listing.php
文件夹 /images/
内所有文件的名称代码>?
谢谢。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
请参阅: readdir()
See: readdir()
使用 glob 甚至比 readdir() 更容易:有关
glob 的更多信息
Even easier than readdir(), use glob:
more info on glob
使用
scandir
或dir
使这个问题变得微不足道。要获取目录中除特殊文件.
和..
之外的所有文件(索引从0
开始的数组),可以组合scandir
与array_diff
和array_merge
:Using either
scandir
ordir
makes this problem trivial. To get all files in a directory except the special files.
and..
in an array with indices starting from0
, one can combinescandir
witharray_diff
andarray_merge
:这是使用 SPL DirectoryIterator 类的方法:
Here is a method using the SPL DirectoryIterator class :
只是扩展恩里科的帖子,您还需要做一些检查/修改。
用法如下:
这将为您提供一个如下所示的列表:
希望这会有所帮助。
just extending on Enrico's post, theres also some checks/modifications you need to do.
And the usage like so:
This will get you a list like so:
hoep this helps.