1

I have this code for show .log files names and extension from directory like this:

error_2014-11-06.log

CODE:

$files = glob("../cache/logs/*.log", 1);
foreach ($files as $filename){
?>

<div><?PHP echo $filename;?></div>

<?PHP } ?>

Now i see this error :

[E_WARNING] [2] glob(): At least one of the passed flags is invalid or not supported on this platform in
Pink Code
  • 1,802
  • 7
  • 43
  • 65

3 Answers3

1
$files = glob("../cache/logs/*.log", 1);
-------------------------------------^

This is not a valid flag. The available valid flags are here

Available Flags:

  • GLOB_MARK - Adds a slash to each directory returned
  • GLOB_NOSORT - Return files as they appear in the directory (no sorting). When this flag is not used, the pathnames are sorted alphabetically
  • GLOB_NOCHECK - Return the search pattern if no files matching it were found
  • GLOB_NOESCAPE - Backslashes do not quote metacharacters
  • GLOB_BRACE - Expands {a,b,c} to match 'a', 'b', or 'c'
  • GLOB_ONLYDIR - Return only directory entries which match the pattern
  • GLOB_ERR - Stop on read errors (like unreadable directories), by default errors are ignored.

To use any of them, simply do

glob("path", GLOB_MARK); // example
Ali
  • 3,479
  • 4
  • 16
  • 31
  • This is incorrect. 1 corresponds to `GLOB_ERR`. But using `GLOB_ERR` would not have prevented that error. While it's better to use the constant in any case instead of the value directly, the error is caused because `GLOB_ERR` was added in PHP 5.1 and the OP is apparently using an older version. – Gordon Nov 13 '15 at 08:32
1

The number 1 corresponds to GLOB_ERR, which was added in PHP 5.1.0 (see Changelog section). If you get this error, you are using an outdated version of PHP.

Consider upgrading to a version that is not End of Life.

Note that you would also get this error if you had used the constant in the first place. PHP doesn't care whether you use the flag name or value. As you can see from the OPCodes, PHP will send the value to glob anyway:

Code: glob('foo', GLOB_ERR);

Finding entry points
Branch analysis from position: 0
Jump found. Position 1 = -2
filename:       /in/n0vqf
function name:  (null)
number of ops:  4
compiled vars:  none
line     #* E I O op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
   3     0  E >   SEND_VAL                                               'foo'
         1        SEND_VAL                                               1
         2        DO_FCALL                                    2          'glob'
         3      > RETURN                                                 1

The reason why you want to use the constant is because it is more readable than a magic number. Also, relying on the constant is more stable in case the value gets changed for some technical reason.

You can use this code to get the values for the GLOB_* constants:

foreach (get_defined_constants() as $k => $v) { 
    if (strpos($k, "GLOB") === 0) { 
        echo "$k => $v", PHP_EOL;
    }
}

Output (PHP 5.6.15):

GLOB_BRACE => 1024
GLOB_MARK => 2
GLOB_NOSORT => 4
GLOB_NOCHECK => 16
GLOB_NOESCAPE => 64
GLOB_ERR => 1
GLOB_ONLYDIR => 8192
GLOB_AVAILABLE_FLAGS => 9303

For further reference, see the implementation of glob at

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
-1

the second parameter (, 1) should be a constant from the following list, but you probably don't need one at all

GLOB_MARK, GLOB_NOSORT, GLOB_NOCHECK, GLOB_NOESCAPE, GLOB_BRACE, GLOB_ONLYDIR, GLOB_ERR.

http://php.net/manual/en/function.glob.php

Sergey
  • 494
  • 2
  • 6
  • PHP will pass the constant value to `glob`, so it doesn't matter whether you use the constant or the constant value directly. It's better practise to use the constant, but using the constant in this case would not prevent that error. – Gordon Nov 13 '15 at 08:55