Checking for file-extensions in PHP with Regular expressions

前端 未结 7 2128
伪装坚强ぢ
伪装坚强ぢ 2020-12-28 17:30

I\'m reading all the files in a single directory and I want to filter on JPG,JPEG,GIF and PNG.

Both capital and small letters. Those are the only files to be accepte

7条回答
  •  佛祖请我去吃肉
    2020-12-28 17:52

    Here are two different ways to compile an array of files by type (conf for demo) from a target directory. I'm not sure which is better performance wise.

        $path    = '/etc/apache2/';
        $conf_files = []; 
    
        // Remove . and .. from the returned array from scandir
        $files = array_diff(scandir($path), array('.', '..'));
        foreach($files as $file) {
            if(in_array(pathinfo($file, PATHINFO_EXTENSION), ['conf'])) {
                $conf_files[] = $file;   
            }   
        }
        return $conf_files;
    

    This will return the full file path not just the file name

    return $files = glob($path . '*.{conf}',GLOB_BRACE);
    

提交回复
热议问题