Glob() 文件搜索,问题
有一个小问题。我有这段代码,它非常适合文件,但如果我尝试搜索目录名称,结果为空。我该如何解决这个问题?
<?php
function listdirs($dir,$search)
{
static $alldirs = array();
$dirs = glob($dir."*");
foreach ($dirs as $d){
if(is_file($d)){
$filename = pathinfo($d);
if(eregi($search,$filename['filename'])){
print "<a href=http://someurl.com/" . $d .">". $d . "</a><br/>";
}
}else{
listdirs($d."/",$search);
}
}
}
$path = "somedir/";
$search= "test";
listdirs($path,$search);
?>
somedir/test/
结果:空白(我想要:/somedir/test/)
somedir/test/test.txt
结果:好的
我也想在目录名称中搜索,我该怎么做?
a little question. I have this code, which is works perfect for files, but If am trying search on a directory name, the result is blank. How I can fix that?
<?php
function listdirs($dir,$search)
{
static $alldirs = array();
$dirs = glob($dir."*");
foreach ($dirs as $d){
if(is_file($d)){
$filename = pathinfo($d);
if(eregi($search,$filename['filename'])){
print "<a href=http://someurl.com/" . $d .">". $d . "</a><br/>";
}
}else{
listdirs($d."/",$search);
}
}
}
$path = "somedir/";
$search= "test";
listdirs($path,$search);
?>
somedir/test/
result: blank (I want: /somedir/test/)
somedir/test/test.txt
result: OK
I want to search also in the directory names, how I can do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想搜索目录,则必须更改
if(is_file($d))
块。现在,当它遇到目录时,您只需再次调用listdirs
...但这也意味着您永远不会看到带有指向所述目录的链接的print
目录。我建议在
foreach
中执行类似的操作:If you want to search for a directory, you're going to have to change the
if(is_file($d))
block. Right now, you're having it simply calllistdirs
again when it encounters a directory... but this also means you'll never see aprint
with a link to said directory.I suggest doing something like this in the
foreach
instead:你的脚本运行良好。我认为网络服务器用户没有给定目录的权限。
Your script is working fine. I think the webserver user does not have permissions to the given directory.