PHP glob (): at least one of the passed flags is invalid or not supported on this platform in

I have this code to display .log file names and extensions from a directory as follows:

 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 
+1
php
Nov 05 '14 at 17:13
source share
3 answers
 $files = glob("../cache/logs/*.log", 1); -------------------------------------^ 

This is not a valid flag. Valid flags available here

Available flags:

  • GLOB_MARK - Adds a slash to each returned directory
  • GLOB_NOSORT - returns files as they appear in the directory (no sorting). When this flag is not used, path names are sorted alphabetically.
  • GLOB_NOCHECK - Returns the search pattern if no files matching it are found.
  • GLOB_NOESCAPE - backslashes do not quote metacharacters
  • GLOB_BRACE - Extends {a, b, c} to match 'a', 'b' or 'c'
  • GLOB_ONLYDIR - returns only directory entries matching the pattern
  • GLOB_ERR - Stop read errors (for example, unreadable directories), errors are ignored by default.

To use any of them, simply do

 glob("path", GLOB_MARK); // example 
+1
Nov 05 '14 at 17:19
source share

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

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

Please note that you will also get this error if you used a constant in the first place. PHP doesn't care if you use a flag name or value. As you can see from OPCodes, PHP will still send the value to glob :

 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 #* EIO 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 you want to use a constant is because it is more readable than a magic number . In addition, relying on a constant, it is more stable if the value changes for some technical reason.

This code can be used to get the GLOB_* constants:

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

Exit (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 more information, see the glob implementation in

0
Nov 13 '15 at 8:46
source share

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

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

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

-one
Nov 05 '14 at 17:18
source share



All Articles