PHP parse directory and subdirectories for file paths and names of only jpg image types

前端 未结 1 767
后悔当初
后悔当初 2021-01-14 04:14

I am looking to modify this php code to do a recursive \"search for and display image\" on a single, known, directory with an unknown amount of sub-directories.

He

相关标签:
1条回答
  • 2021-01-14 04:58

    Some time ago I wrote this function to traverse a directory hierarchy. It will return all file paths contained in the given folder (but not folder paths). You should easily be able to modify it to return only files whose name ends in .jpg.

    function traverse_hierarchy($path)
    {
        $return_array = array();
        $dir = opendir($path);
        while(($file = readdir($dir)) !== false)
        {
            if($file[0] == '.') continue;
            $fullpath = $path . '/' . $file;
            if(is_dir($fullpath))
                $return_array = array_merge($return_array, traverse_hierarchy($fullpath));
            else // your if goes here: if(substr($file, -3) == "jpg") or something like that
                $return_array[] = $fullpath;
        }
        return $return_array;
    }
    
    0 讨论(0)
提交回复
热议问题