如何使用PHP从文件夹中读取文件列表? [英] How to read a list of files from a folder using PHP?

查看:67
本文介绍了如何使用PHP从文件夹中读取文件列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用php读取网页中文件夹中文件的名称。
有任何简单的脚本来实现吗?

I want to read a list the names of files in a folder in a web page using php. is there any simple script to acheive it?

推荐答案

最简单和最有趣的方式(imo)是 glob

The simplest and most fun way (imo) is glob

foreach (glob("*.*") as $filename) {
    echo $filename."<br />";
}

但标准的方法是使用目录函数

if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo "filename: .".$file."<br />";
        }
        closedir($dh);
    }
}

还有一个 SPL DirectoryIterator方法。如果您有兴趣

There are also the SPL DirectoryIterator methods. If you are interested

这篇关于如何使用PHP从文件夹中读取文件列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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