PHP 目录中特定文件的列表
以下代码将列出目录中的所有文件,
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle)))
{
if (($file != ".")
&& ($file != ".."))
{
$thelist .= '<LI><a href="'.$file.'">'.$file.'</a>';
}
}
closedir($handle);
}
?>
<P>List of files:</p>
<UL>
<P><?=$thelist?></p>
</UL>
虽然这是非常简单的代码,但它可以完成这项工作。
我现在正在寻找一种方法来仅列出末尾带有 .xml(或 .XML)的文件,我该怎么做?
The following code will list all the file in a directory
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle)))
{
if (($file != ".")
&& ($file != ".."))
{
$thelist .= '<LI><a href="'.$file.'">'.$file.'</a>';
}
}
closedir($handle);
}
?>
<P>List of files:</p>
<UL>
<P><?=$thelist?></p>
</UL>
While this is very simple code it does the job.
I'm now looking for a way to list ONLY files that have .xml (or .XML) at the end, how do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您将需要使用
glob()
示例:
You'll be wanting to use
glob()
Example:
使用 substr 和 strrpos 查看扩展的简单方法
A simple way to look at the extension using substr and strrpos
您还可以使用迭代器的递归变体来遍历整个目录层次结构。
You can also use the recursive variants of the iterators to traverse an entire directory hierarchy.
我使用这段代码:
I use this code:
你可以混合使用 glob() 函数和pathinfo() 函数如下所示。
下面的代码将显示特定扩展名“pdf”的文件信息
you can mix between glob() function & pathinfo() function like below.
the below code will show files information for specific extension "pdf"
列出目录中的文件和文件夹(完整代码):
如果您只想特定扩展,则必须取消第 5 行注释
LIST FILES and FOLDERS in a directory (Full Code):
p.s. you have to uncomment the 5th line if you want only for specific extensions
你应该使用全局。
有关使用 glob 和高级过滤的更多信息:
http://domexception .blogspot.fi/2013/08/php-using-function-programming-for.html
You should use glob.
More about using glob and advanced filtering:
http://domexception.blogspot.fi/2013/08/php-using-functional-programming-for.html
$files 数组将获取指定扩展名目录下的所有文件
The $files array will get all files in the directory which the specified extension
最简单的答案是设置另一个条件
'.xml' == strtolower(substr($file, -3))
。但我也建议使用
glob
来代替。Simplest answer is to put another condition
'.xml' == strtolower(substr($file, -3))
.But I'd recommend using
glob
instead too.您可以像这样扩展 RecursiveFilterIterator 类:
现在您可以使用路径作为参数实例化 RecursiveDirectoryIterator,如下所示:
这将仅列出当前文件夹下的文件。
要获取子目录中的文件,
将 $iterator (ExtensionFIlter Iterator) 作为参数传递给 RecursiveIteratorIterator:
现在在此迭代器上运行 foreach 循环。您将获得具有指定扩展名的文件
注意:-- 另外请确保在 RecursiveIteratorIterator 之前运行 ExtensionFilter,否则您将获得所有文件
You can extend the RecursiveFilterIterator class like this:
Now you can instantiate RecursiveDirectoryIterator with path as an argument like this:
This will list files under the current folder only.
To get the files in subdirectories also,
pass the $iterator ( ExtensionFIlter Iterator) to RecursiveIteratorIterator as argument:
Now run the foreach loop on this iterator. You will get the files with specified extension
Note:-- Also make sure to run the ExtensionFilter before RecursiveIteratorIterator, otherwise you will get all the files