当路径目录带有方括号时,我在使用 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>';
上面的代码可以工作,但是当目录名称带有方括号(例如 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>';
谢谢大家。
我得到了我的查询的确切解决方案。下面的代码对我有用
$path = 'temp'. DIRECTORY_SEPARATOR .'dir - [name]';
$path = str_replace('[', '\[', $path);
$path = str_replace(']', '\]', $path);
$path = str_replace('\[', '[[]', $path);
$path = str_replace('\]', '[]]', $path);
$files = glob($path . DIRECTORY_SEPARATOR . '*.txt');
// List files
echo '<pre>';
print_r($files);
echo '</pre>';
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)