龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

PHP遍历指定目录下所有文件函数,可指定文件类

时间:2014-07-22 14:50来源: 作者: 点击:
分享到:
这个比 span style=font-family:Consolas, Bitstream Vera Sans Mono, Courier New, Courier, monospace;line-height:14.649999618530273px;background-color:#F8F8F8;scandir出来的好用/span
这个比 scandir出来的好用
    /**
     * 遍历获取目录下的指定类型的文件
     * @param $path
     * @param array $files
     * @return array
     */
    function getfiles( $path , &$files = array() )
    {
        if ( !is_dir( $path ) ) return null;
        $handle = opendir( $path );
        while ( false !== ( $file = readdir( $handle ) ) ) {
            if ( $file != '.' && $file != '..' ) {
                $path2 = $path . '/' . $file;
                if ( is_dir( $path2 ) ) {
                    getfiles( $path2 , $files );
                } else {
                    if ( preg_match( "/\.(gif|jpeg|jpg|png|bmp)$/i" , $file ) ) {
                        $files[] = $path2;
                    }
                }
            }
        }
        return $files;
    }
精彩图集

赞助商链接