使用PHP获取目录中所有文件的名称 [英] Getting the names of all files in a directory with PHP

查看:32
本文介绍了使用PHP获取目录中所有文件的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,我一直使用此代码为文件名获取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);
    }
}

当我回显 $results_array 中的每个元素时,我得到一堆1",而不是文件名.如何获取文件名?

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?

推荐答案

不要打扰 open/readdir 使用 glob 代替:

Don't bother with open/readdir and use glob instead:

foreach(glob($log_directory.'/*.*') as $file) {
    ...
}

这篇关于使用PHP获取目录中所有文件的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆