Checking for file-extensions in PHP with Regular expressions

前端 未结 7 2068
伪装坚强ぢ
伪装坚强ぢ 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

    I think something is wrong with your regex. Try testing regexes here first: https://www.regexpal.com/

    I think this one might work for you:

    /^.*\.(jpg|jpeg|png|gif)$/i

    Note the /i at the end - this is the "case insensitive" flag, saves you having to type out all permutations :)

    0 讨论(0)
  • 2020-12-28 17:52

    How about using glob() instead?

    $files = glob($dir . '*.{jpg,gif,png,jpeg}',GLOB_BRACE);
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-28 17:54

    You should put slashes around your regexp. -> "/(...)/"

    0 讨论(0)
  • 2020-12-28 17:55

    Is there any reason you don't want to use scandir and pathinfo?

    public function scanForFiles($path, array $exts)
    {
        $files = scanDir($path);
    
        $return = array();
    
        foreach($files as $file)
        {
            if($file != '.' && $file != '..')
            {
                if(in_array(pathinfo($file, PATHINFO_EXTENSION), $exts))) {
                    $return[] = $file;   
                }
            }
        }
    
        return $return;
    }
    

    So you could also check if the file is a directory and do a recursive call to scan that directory. I wrote the code in haste so might not be 100% correct.

    0 讨论(0)
  • 2020-12-28 17:57

    There are a few ways of doing this.

    Have you tried glob()?:

    $files = glob("{$picsDir}/*.{gif,jpeg,jpg,png}", GLOB_BRACE);
    

    Have you considered pathinfo()?:

    $info = pathinfo($file);
    switch(strtolower($info['extension'])) {
        case 'jpeg':
        case 'jpg':
        case 'gif':
        case 'png':
            $files[] = $file;
            break;
    }
    

    If you're insistant upon using the regular expression, there's no need to match the whole filename, just the extension. Use the $ token to match the end of the string, and use the i flag to denote case-insensitivity. Also, don't forget to use a delimiter in your expression, in my case "%":

    $rex = '%\.(gif|jpe?g|png)$%i';
    
    0 讨论(0)
提交回复
热议问题