panado Posted November 25, 2009 Share Posted November 25, 2009 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 Link to comment https://forums.phpfreaks.com/topic/182928-trying-to-create-a-filtered-search-result-on-a-file-directory/ Share on other sites More sharing options...
Goldeneye Posted November 25, 2009 Share Posted November 25, 2009 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>"; } ?> Link to comment https://forums.phpfreaks.com/topic/182928-trying-to-create-a-filtered-search-result-on-a-file-directory/#findComment-965528 Share on other sites More sharing options...
panado Posted November 25, 2009 Author Share Posted November 25, 2009 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 Link to comment https://forums.phpfreaks.com/topic/182928-trying-to-create-a-filtered-search-result-on-a-file-directory/#findComment-965537 Share on other sites More sharing options...
Goldeneye Posted November 25, 2009 Share Posted November 25, 2009 Try... $ext = strtolower($extArr[count($extArr)-1]); Link to comment https://forums.phpfreaks.com/topic/182928-trying-to-create-a-filtered-search-result-on-a-file-directory/#findComment-965551 Share on other sites More sharing options...
panado Posted November 25, 2009 Author Share Posted November 25, 2009 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. Link to comment https://forums.phpfreaks.com/topic/182928-trying-to-create-a-filtered-search-result-on-a-file-directory/#findComment-965583 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.