获取目录中图像的文件名

Ham*_*lid 7 php opendir

应该怎么做才能使用PHP从文件夹/目录中获取图像的标题(例如abc.jpg)并将它们存储在数组中.

例如:

a[0] = 'ac.jpg'
a[1] = 'zxy.gif'
Run Code Online (Sandbox Code Playgroud)

等等

我将在幻灯片放映中使用该数组.

Leo*_*ard 12

这当然是可能的.查看opendir的文档并将每个文件推送到结果数组.如果您使用的是PHP5,请查看DirectoryIterator.遍历目录内容是一种更流畅,更清晰的方式!

编辑:opendir上构建:

$dir = "/etc/php5/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        $images = array();

        while (($file = readdir($dh)) !== false) {
            if (!is_dir($dir.$file)) {
                $images[] = $file;
            }
        }

        closedir($dh);

        print_r($images);
    }
}
Run Code Online (Sandbox Code Playgroud)


Eve*_*ert 5

'scandir'这样做:

$images = scandir($dir);
Run Code Online (Sandbox Code Playgroud)

  • 为什么这不是公认的答案?!美丽的一个班轮. (2认同)

ajr*_*eal 5

一个班轮: -

$arr = glob("*.{jpg,gif,png,bmp}", GLOB_BRACE) 
Run Code Online (Sandbox Code Playgroud)