PhP搜索目录按文件扩展名

时间:2014-11-21 14:33:48

标签: php file directory

我有一个脚本,其中的一个函数获取该文件夹中的每个文件。如何编辑此代码以使其仅获取.png文件?

public function GetPreviews($category)
    {
        $directories = glob("assets/preview/$category/*");
        $directory = array();
        foreach($directories as $directories)
        {
            $directory[] = str_replace("assets/preview/$category/", "", $directories);   
        }
        return $directory;      
    }

3 个答案:

答案 0 :(得分:1)

将.png添加到glob(" assets / preview / $ category / * .png");

public function GetPreviews($category)
    {
        $directories = glob("assets/preview/$category/*.png");
        $directory = array();
        foreach($directories as $directories)
        {
            $directory[] = str_replace("assets/preview/$category/", "", $directories);   
        }
        return $directory;      
    }

答案 1 :(得分:0)

由于您使用的是glob() - 只需在最后.png之后添加*

像这样:

public function GetPngs($category)
    {
        $directories = glob("assets/preview/$category/*.png");
        $directory = array();
        foreach($directories as $directories)
        {
            $directory[] = str_replace("assets/preview/$category/", "", $directories);   
        }
        return $directory;      
    }

答案 2 :(得分:0)

你缺少' .png'在您的代码中只需更改

$directories = glob("assets/preview/$category/*");

$directories = glob("assets/preview/$category/*.png");