1

I try to fix my search code so that not just files with ".png" extensions will be found, but also all files with ".map, .jpg, .gif".
When i delete the '.png'; people have to type in the full filename like "test.png"...

Code:

<?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=""/>';
}

?>

I hope you can help me. Thanks

  • 1
    If you want to search by multiple patterns instead of a single one then obviously you have to change the code such that it iterates over all those patterns. You need a loop. – arkascha Sep 30 '16 at 12:26
  • Have you tried to run mutiple searches (one for every format) and merge the results after all searches are done ? – Yannick Huber Sep 30 '16 at 12:27
  • Though I never understand why people are so keen on handling "file name extensions" at all. They are more or less meaning less in modern operating systems (with one notable exception...). The fact that a file name ends with `.png` does not really say anything about its content. – arkascha Sep 30 '16 at 12:27
  • Oh it works when I run multiple searches one for each format. Thanks a lot! :) –  Sep 30 '16 at 12:36

0 Answers0