8

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

例如:

a[0] = 'ac.jpg'
a[1] = 'zxy.gif'

等等

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

4

4 回答 4

13

这当然是可能的。查看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);
    }
}
于 2011-12-07T11:47:16.890 回答
5

'scandir' 这样做:

$images = scandir($dir);
于 2011-12-07T11:50:46.917 回答
5

一个班轮:-

$arr = glob("*.{jpg,gif,png,bmp}", GLOB_BRACE) 
于 2011-12-07T11:53:30.377 回答
4

php中的glob -查找匹配模式的路径名

<?php
    //path to directory to scan
    $directory = "../images/team/harry/";
    //get all image files with a .jpg extension. This way you can add extension parser
    $images = glob($directory . "{*.jpg,*.gif}", GLOB_BRACE);
    $listImages=array();
    foreach($images as $image){
        $listImages=$image;
    }
?>
于 2011-12-07T11:50:38.557 回答