0

I have this script that works good, but I need to add a command for searching on all subfolders.

Example: I have a folder data and this contains more another folders... I need to search files on this folders.

$dir = 'data'; 
$exclude = array('.', '..', '.htaccess'); 
$q = (isset($_GET['q'])) ? strtolower($_GET['q']) : ''; 
$res = opendir($dir); 

while(false !== ($file = readdir($res))) { 
    if(strpos(strtolower($file), $q) !== false &&!in_array($file, $exclude)) { 
        echo "<a href='$dir/$file'>$file</a>"; 
        echo "<br>"; 
    } 
} 

closedir($res); 
Kévin Bibollet
  • 3,573
  • 1
  • 15
  • 33
  • Possible duplicate : https://stackoverflow.com/questions/12109042/php-get-file-listing-including-sub-directories – Kisaragi May 07 '19 at 05:07
  • 3
    Possible duplicate of [PHP get file listing including sub directories](https://stackoverflow.com/questions/12109042/php-get-file-listing-including-sub-directories) – Kisaragi May 07 '19 at 05:07

2 Answers2

0

you can use scandir() function

$dir  = '/tmp';
$file = scandir($dir);
print_r($file);
PHP Geek
  • 3,949
  • 1
  • 16
  • 32
  • Thanks but this no show me rezult on folder after use search from.This show me Array ( [0] => . [1] => .. [2] => pdf1 [3] => pdf2 [4] => pdf3 ) I need search and show file from folders the pdf1,pdf2 and pdf3 is me.folders.Thanks you – Ionutz Cosmin May 07 '19 at 15:31
  • I need help for add message notice on original script if the file not exist after search.Sample : File not found. – Ionutz Cosmin May 14 '19 at 09:04
  • try this if (file_exists($filename)) { echo "success";}else{ echo "The file $filename does not exist"; } – PHP Geek May 14 '19 at 09:21
  • Thanks! Please is posisble add the message "Wait file searching..." message on the script before file is found? – Ionutz Cosmin May 14 '19 at 16:56
0

I think this is the recursive function you are looking for :

function dir_walk($dir, $q) {
    $q = trim($q);
    $exclude = array('.', '..', '.htaccess'); 
    if($dh = opendir($dir)) {
        while(($file = readdir($dh)) !== false) {
            if(in_array($file, $exclude)) { continue; }
            elseif(is_file($dir.$file)) {
                if($q === '' || strpos(strtolower($file), $q) !== false) { 
                    echo '<a href='.$dir.$file.'>'.$dir.$file.'</a><br/>';
                    }
                }
            elseif(is_dir($dir.$file)) {
                dir_walk($dir.$file.DIRECTORY_SEPARATOR, $q);
                }
            }
        closedir($dh);
        }
    }

$q = (isset($_GET['q'])) ? strtolower($_GET['q']) : ''; 
dir_walk('/data/', $q);

Edit: data need to be the absolute path to the main dir, with ending directory separator "/data/"

lainderon
  • 95
  • 8
  • Thanks you,but not.worked..nothing rezult found..I need search file on folders and subfolders.Example I have folder data and this contains pdf1,pdf2 and pdf3 folders...I need search files on this pdf1..pdf2..pdf3... Thanks. – Ionutz Cosmin May 07 '19 at 16:46
  • Here it works, but i have to put the absolute path, with ending directory separator, tested on windows with 'c:\\data\\'. Try debug it to match the is_dir() and is_file(). And use $_GET['q'] instead 'mysearch'. – lainderon May 07 '19 at 16:53
  • I use HTML code for runn this php file on xmapp maybe this is problem?
    – Ionutz Cosmin May 07 '19 at 17:18
  • Have improve the code, retry this one. And dont forget to put your absolute/real path instead "/data/". If fail, try it without the form to debug it. Xampp on what OS ? – lainderon May 07 '19 at 17:32
  • I use windows 10 64 bit version. – Ionutz Cosmin May 07 '19 at 17:35
  • So put the absolute path to your main dir, for example like this "``C:\\Xampp\\www\\data\\``" – lainderon May 07 '19 at 17:38
  • Thnaks.is possible please add on me original script operation for search on more folders? On me example I can search only one single folder "data" I need search on more folders,Example data',data1', data2' ...and more.... – Ionutz Cosmin May 08 '19 at 05:55
  • You can add more dir_walk() with your others folders. – lainderon May 08 '19 at 10:52
  • Worked!! I need help how add option for character search minim 3 characters inputs.Me form search minim 1 and max unlimited...I need add limit search form 3 characters typed input..thx – Ionutz Cosmin May 09 '19 at 11:13