当目录名称包含方括号“[]"等特殊字符时,Glob 不起作用. [英] Glob is not working when directory name with special characters like square brackets "[ ]"
本文介绍了当目录名称包含方括号“[]"等特殊字符时,Glob 不起作用.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当路径目录带有方括号时,我在使用 glob 函数时遇到问题.
//示例 1 - 工作$path = '临时'.DIRECTORY_SEPARATOR .'dir - 名称';$files = glob($path .DIRECTORY_SEPARATOR .'*.txt');//列出所有文件echo '';打印_r($文件);echo '</pre>';
上面的代码可以正常工作,但是当目录名称带有方括号(如 dir[name] 或 dir - [name])时,它就不起作用了.
//示例 2 - 不工作$path = '临时'.DIRECTORY_SEPARATOR .'dir - [名称]';$files = glob($path .DIRECTORY_SEPARATOR .'*.txt');//如果该目录中的文件为空,则结果为空echo '';打印_r($文件);echo '</pre>';
解决方案
谢谢大家.
我的查询得到了确切的解决方案.下面的代码对我有用
$path = 'temp'.DIRECTORY_SEPARATOR .'dir - [名称]';$path = str_replace('[', '\[', $path);$path = str_replace(']', '\]', $path);$path = str_replace('\[', '[[]', $path);$path = str_replace('\]', '[]]', $path);$files = glob($path .DIRECTORY_SEPARATOR .'*.txt');//列出文件echo '';打印_r($文件);echo '</pre>';
I have issue while using glob function when path directory with square brackets.
// 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>';
Above code is working but when directory name with square brackets like dir[name] or dir - [name] then its not working.
// 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>';
解决方案
Thanks for all of you.
I got exact solution for my query. below code is a working for me
$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>';
这篇关于当目录名称包含方括号“[]"等特殊字符时,Glob 不起作用.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文