Can PHP's glob() be made to find files in a case insensitive manner?

前端 未结 10 1802
遥遥无期
遥遥无期 2020-11-29 08:15

I want all CSV files in a directory, so I use

glob(\'my/dir/*.CSV\')

This however doesn\'t find files with a lowercase CSV extension.

相关标签:
10条回答
  • 2020-11-29 08:32

    I heard about a function that can be used like this: Try if that works for you!

    <?php
    $pattern = sql_regcase("*.txt");
    glob($pattern);
    ?>
    
    0 讨论(0)
  • 2020-11-29 08:34

    To make it work with all extensions use:

    $extension = 'some_extension';
    glob('my/dir/*.preg_replace('/(\w)/e', "'['.strtoupper($1).strtolower($1).']'", $extension));
    
    0 讨论(0)
  • 2020-11-29 08:42

    Came to this link for glob with multiple files. Although it doesn't help with OP, it may help others who end up here.

    $file_type = 'csv,jpeg,gif,png,jpg';
    $i = '0';
    foreach(explode(",",$file_type) as $row){
        if ($i == '0') {
            $file_types = $row.','.strtoupper($row);
        } else {
            $file_types .= ','.$row.','.strtoupper($row);
        }
        $i++;
    }
    
    $files = glob($dir."*.{".$image_types."}",GLOB_BRACE);
    
    0 讨论(0)
  • 2020-11-29 08:45

    You can write your own case insensitive glob. This is from a personal web library I write:

    /** PHP has no case insensitive globbing
     * so we have to build our own.
     *
     * $base will be the initial part of the path which doesn't need case insensitive
     * globbing.
     * Suffix is similar - it will not be made insensitive
     * Make good use of $base and $suffix to keep $pat simple and fast in use.
     */
        function ciGlob($pat, $base = '', $suffix = '')
        {
            $p = $base;
            for($x=0; $x<strlen($pat); $x++)
            {
                $c = substr($pat, $x, 1);
                if( preg_match("/[^A-Za-z]/", $c) )
                {
                    $p .= $c;
                    continue;
                }
                $a = strtolower($c);
                $b = strtoupper($c);
                $p .= "[{$a}{$b}]";
            }
            $p .= $suffix;
            return glob($p);
        }
    
    0 讨论(0)
提交回复
热议问题