PHP glob () in brackets

On a Windows computer, the following script:

<?php mkdir("c:\\[test]"); file_put_contents("c:\\[test]\\test.txt", "some content"); chdir("c:\\[test]"); echo getcwd()."\n"; var_dump(glob('*')); ?> 

Displays this:

 C:\[test] array(0) { } 

When it is expected:

 C:\[test] array(1) { [0]=> string(8) "test.txt" } 

I understand that glob treats parentheses as special characters found in the pattern parameter.

Sample * matches any file in the current working directory. However, glob () behaves as if it started with the c:\\[test]\\* pattern

The brackets are then interpreted as part of the pattern, when in fact they are part of the directory.

Does glob intend to consider the path as part of the pattern? I would prefer it to use the current directory as a starting point, and then only process the template.

(Attempt to generalize): The glob function acts as if it receives c:\\[test]\\* as a matching pattern and tries to match either c:\t\* , c:\e\* or c:\s\* . But the pattern is really * , and he should not try to match this.

+6
source share
1 answer

This seems to be seen as a problem in this error report on php.net: https://bugs.php.net/bug.php?id=33047

The last post in this thread about this is not an error, but the problem is how glob treats parentheses as part of a regular expression. I am not sure I agree. It seems you can get around this if you cannot move to the parent folder.

If you delete the first requirement for the presence in the internal [test] folder, you can get the list of files using the syntax as shown below:

 chdir('..'); $glob = glob("[[]test[]]/*"); 

Given these complications, I would recommend not to use the glob function if you encounter problems on Windows machines, and look at the other file transfer functions such as readdir .

+4
source

Source: https://habr.com/ru/post/902897/


All Articles