当路径目录带有方括号时,我在使用 glob 函数时遇到问题。
// Example 1 - working
$path = 'temp'. DIRECTORY_SEPARATOR .'dir - name';
$files = glob($path . DIRECTORY_SEPARATOR . '*.txt');
// List all files
echo '<pre>';
print_r($files);
echo '</pre>';
Run Code Online (Sandbox Code Playgroud)
上面的代码可以正常工作,但是当目录名称带有方括号(如 dir[name] 或 dir - [name])时,它就不起作用了。
// Example 2 - not working
$path = 'temp'. DIRECTORY_SEPARATOR .'dir - [name]';
$files = glob($path . DIRECTORY_SEPARATOR . '*.txt');
// result got empty if file on that directory
echo '<pre>';
print_r($files);
echo '</pre>';
Run Code Online (Sandbox Code Playgroud)