php - $ _GET搜索扩展程序

时间:2016-09-30 12:23:42

标签: php file search glob

我尝试修复我的搜索代码,以便不仅可以找到带有“.png”扩展名的文件,还可以找到所有带有“.map,.jpg,.gif”文件的文件。
当我删除'.png'时;人们必须输入完整的文件名,如“test.png”......

代码:

<?php
$dirname = 'assets/db/';
$findme  = $_GET['search']. '.png';  // this is where I add the ".png" manually...
$dirs    = glob($dirname.'*', GLOB_ONLYDIR);

$files   = array();

function findAllDirs($start) {
    $dirStack=[$start];
    while($dir=array_shift($dirStack)) {
        $ar=glob($dir.'/*',GLOB_ONLYDIR|GLOB_NOSORT);
        if(!$ar) continue;

        $dirStack=array_merge($dirStack,$ar);
        foreach($ar as $DIR)
            yield $DIR;
    }
}

$result= [];

foreach(findAllDirs($dirname) as $d) {
    $f = glob( $d .'/'. $findme );
    if( count( $f ) ) {
        $files = array_merge( $files, $f );
    }
}
if( count($files) ) {
    foreach( $files as $f ) {
        echo "<h3> <a href='{$f}'> $findme </a>"; 
        

     echo "<img src='$f'/>";
    
        echo "</h3> <hr> <br>";
    }
} else {
    echo "<h3> Nothing was found. Sorry. </h3>";
    echo '<img src=""/>';
}

?>

我希望你能帮助我。感谢

0 个答案:

没有答案