使用 PHP 获取目录中所有文件的名称
由于某种原因,我使用以下代码不断获得文件名“1”:
if (is_dir($log_directory))
{
if ($handle = opendir($log_directory))
{
while($file = readdir($handle) !== FALSE)
{
$results_array[] = $file;
}
closedir($handle);
}
}
当我回显 $results_array 中的每个元素时,我得到一堆“1”,而不是文件名。如何获取文件的名称?
For some reason, I keep getting a '1' for the file names with this code:
if (is_dir($log_directory))
{
if ($handle = opendir($log_directory))
{
while($file = readdir($handle) !== FALSE)
{
$results_array[] = $file;
}
closedir($handle);
}
}
When I echo each element in $results_array, I get a bunch of '1's, not the name of the file. How do I get the name of the files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
不要打扰 open/readdir 并使用
glob
相反:Don't bother with open/readdir and use
glob
instead:SPL 样式:
检查 DirectoryIterator 和 SplFileInfo 类,获取您可以使用的可用方法的列表。
SPL style:
Check DirectoryIterator and SplFileInfo classes for the list of available methods that you can use.
由于已接受的答案有两个重要的缺陷,因此我为那些正在寻找正确答案的新人发布了改进的答案:
is_file
过滤glob
函数结果是必要的,因为它也可能返回一些目录。.
,因此*/*
模式通常很糟糕。As the accepted answer has two important shortfalls, I'm posting the improved answer for those new comers who are looking for a correct answer:
glob
function results withis_file
is necessary, because it might return some directories as well..
in their names, so*/*
pattern sucks in general.您需要用括号将
$file = readdir($handle)
括起来。干得好:
You need to surround
$file = readdir($handle)
with parentheses.Here you go:
只需使用
glob('*')
。这是文档Just use
glob('*')
. Here's Documentation我有更小的代码来做到这一点:
I have smaller code todo this:
在某些操作系统上,您会得到
.
..
和.DS_Store
,我们不能使用它们,所以让我们隐藏它们。首先使用
scandir()
获取有关文件的所有信息On some OS you get
.
..
and.DS_Store
, Well we can't use them so let's us hide them.First start get all information about the files, using
scandir()
这是由于操作员的优先级。尝试将其更改为:
It's due to operator precidence. Try changing it to:
glob()
和FilesystemIterator
示例:glob()
andFilesystemIterator
examples:您可以尝试使用
scandir(Path)
函数。它实现起来既快速又简单。语法:
该函数将文件列表返回到数组中。
要查看结果,您可以尝试
或
You could just try the
scandir(Path)
function. it is fast and easy to implementSyntax:
This Function returns a list of file into an Array.
to view the result, you can try
Or
列出目录和文件的另一种方法是使用此处回答的
RecursiveTreeIterator
:https://stackoverflow.com/ a/37548504/2032235。关于 PHP 中的
RecursiveIteratorIterator
和迭代器的完整解释可以在这里找到:https://stackoverflow.com/a /12236744/2032235Another way to list directories and files would be using the
RecursiveTreeIterator
answered here: https://stackoverflow.com/a/37548504/2032235.A thorough explanation of
RecursiveIteratorIterator
and iterators in PHP can be found here: https://stackoverflow.com/a/12236744/2032235我只是使用这段代码:
I just use this code:
使用:
来源:http: //chandreshrana.blogspot.com/2016/08/how-to-fetch-all-files-name-from-folder.html
Use:
Source: http://chandreshrana.blogspot.com/2016/08/how-to-fetch-all-files-name-from-folder.html
递归代码探索目录中包含的所有文件(“$path”包含目录的路径):
Recursive code to explore all the file contained in a directory ('$path' contains the path of the directory):
我为此创建了一些小东西:
Little something I created for this:
这将列出文件并创建在新窗口中打开的链接。就像常规服务器索引页一样:
This will list the files and create links that open in a new window. Just like a regular server index page: