Jump to content

Trying to create a filtered search result on a file directory


panado

Recommended Posts

Hi All,

 

I am new to php, I have used the following script by GuiltyGear:

<?php

if(isset($_POST['input'])){

  $input = $_POST['input'];

  $dir = 'mydir';

  $dirHandle = opendir($dir);

  while($files = readdir($dirHandle)){

      if($files == '.' or $files == '..'){ continue; }

      if(strstr($files, $input)){

        echo "<a href=\"showFiles.php?dir=$dir&file=$files\">$files</a>";

      }

  }

  closedir($dirHandle);

}

?>

 

What I am trying to find out is if I can filter the results to only show the .pdf & .jpg files that match the search within the specified folder?

 

Any assistance would be great.

 

Thank you

Make a $_GET['filter'] variable

 

And then make $_GET['filter'] hold a file extension like "pdf" or "jpg"

 

<?php
      $extArr = explode('.', basename($files));
$ext = strtolower(last($extArr));
      if(strstr($files, $input) || (isset($_GET['filter']) && ($ext == $_GET['filter']) && strstr($files, $input))){
         echo "<a href=\"showFiles.php?dir=$dir&file=$files\">$files</a>";
      }

?>

 

Or, instead of using a $_GET variable, you can just put the file-types you want to show in an array.

 

<?php
//define the array before the while-loop
$show_types = array('pdf', 'jpg');

//Now, skip to the while-loop
      $extArr = explode('.', basename($files));
$ext = strtolower(last($extArr));
      if(strstr($files, $input) && in_array($ext, $show_types)){
         echo "<a href=\"showFiles.php?dir=$dir&file=$files\">$files</a>";
      }

?>

Goldeneye,

 

Thanks for the response, I am trying the second option, I now get an error message :

Fatal error: Call to undefined function last() in /var/www/intranet/dev/t5/2.php on line 17

 

Line 17

$ext = strtolower(last($extArr));

 

Am I missing something?

thanks

Perfect, thank you so much.

 

Now I am being greedy but if I wanted this to search multiple folders is that possible?

 

Currently it searches a single folder but if I have files in 2 or more folders can it search all of them or do I need to search them independently?

 

Thanks again.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.