Glob() filesearch, question
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 t开发者_C百科he directory names, how I can do that?
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 call listdirs
again when it encounters a directory... but this also means you'll never see a print
with a link to said directory.
I suggest doing something like this in the foreach
instead:
$filename = basename($d);
if(eregi($search,$filename)){
print "<a href=http://someurl.com/" . $d .">". $d . "</a><br/>";
}
if(is_dir($d)){
listdirs($d."/",$search);
}
Your script is working fine. I think the webserver user does not have permissions to the given directory.
精彩评论