PHP file search lists directory as file -


the following script searching documents/files in specified path. works quite except fact when subfolder has subfolder, lists file/makes clickable. not want. want script show files in each folder (including files in subdirs). hope explanation not messy, haha.

since i'm not able post screenshot try explain.

current folder structure:

vbfiles/     -dir1/           -subdir1/                    -filesubdir1.doc           -subdir2/                    -filesubdir2.doc           -dir1file1.doc           -dir1file2.doc           -dir1file3.doc     -dir2/           -subdir3/                    -filesubdir3.doc           -subdir4/                    -filesubdir4.doc           -dir2file4.doc           -dir2file5.doc           -dir2file6.doc 

result(what want remove marked "<-- no" :)):

found 14 file(s)  dir1  dir1file3.doc  dir1file2.doc  subdir1 <-- no  subdir2 <-- no  dir1file1.doc subdir1  filesubdir1.doc subdir2  filesubdir2.doc dir2  subdir3 <-- no  dir2file4.doc  dir2file5.doc  subdir4 <-- no  dir2file6.doc subdir3  filesubdir3.doc subdir4  filesubdir4.doc 

code:

<?php define("path_root", dirname(__file__) . "/"); define("path_files", path_root . "vbfiles/");  function files($dir) { $files = array(); if ($handle = opendir($dir)) {     while (false !== ($entry = readdir($handle))) {         if ($entry != "." && $entry != "..") {             $files[] = "$entry\n";         }     }     closedir($handle); } return $files; }  function getdirname($name) { $name = substr($name, 0, -1); $x = explode("/", $name); return end($x); }  function read_all_files($root = '.') { $files = array('files' => array(), 'dirs' => array()); $directories = array(); $last_letter = $root[strlen($root) - 1]; $root = ($last_letter == '\\' || $last_letter == '/') ? $root : $root . directory_separator;  $directories[] = $root;  while (sizeof($directories)) {     $dir = array_pop($directories);     if ($handle = opendir($dir)) {         while (false !== ($file = readdir($handle))) {             if ($file == '.' || $file == '..') {                 continue;             }             $file = $dir . $file;             if (is_dir($file)) {                 $directory_path = $file . directory_separator;                 array_push($directories, $directory_path);                 $files['dirs'][] = $directory_path;             } elseif (is_file($file)) {                 $files['files'][] = $file;             }         }         closedir($handle);     } }  return $files; }  $searchstr = $_get['search']; $type = "files";  if ($type == "files") {  $folders = read_all_files(path_files); $found = array(); $count = 0; sort($folders['files']); sort($folders['dirs']); foreach ($folders['dirs'] $folder) {     foreach (files($folder) $filename) {         $m = preg_match("#$searchstr#si", $filename, $ids);         if ($m) {             $found[getdirname($folder)][] = $filename;             $count++;         }     } } $dir = "vbfiles/"; if (count($found)) {     echo "<h3>found " . $count . " file(s)</h3>";     foreach ($found $folder => $files) {         echo "<ul id='navi_lvl0'> <li class='folder'>" . $folder . "</li> <ul id='navi_lvl1'>";          foreach ($files $file) {             echo "<li class='document'><a href='$dir$folder/$file' target='_blank'>" . $file . "</a></li>";         }         echo "</ul></ul>";     } } else {     echo "no files found"; } } ?> 

this function lists files directory. approaches nested child directories also. wrote when required same functionality.

/**  * return files of desired extension directory  *  * @author pankaj garg <garg.pankaj15@gmail.com>  *  * @param string $_dirpath   path directory  * @param string $_extension desired file extension  * @param int    $_before    files before time (in seconds)  * @param int    $_limit     no of files  *  * @return array $_scannedfiles  array of files  */ function scanfilesfromdirectory($_dirpath, $_extension, $_before = 0, $_limit = 10) {     $_scannedfiles = array();      if (empty($_extension)) {         return $_scannedfiles;     }     if (!is_dir($_dirpath)) {         return $_scannedfiles;     }      //if extension starting '.', remove $_extension = (stripos($_extension, '.') === 0) ? substr($_extension, 1) : $_extension; $_handler   = opendir($_dirpath);  while ($_filename = readdir($_handler)) {     //for files starting '.'     if (stripos($_filename, '.') === 0) {         continue;     }      if (is_dir($_dirpath . directory_separator . $_filename)) {         $_scannedfiles = array_merge($_scannedfiles, scanfilesfromdirectory($_dirpath . directory_separator . $_filename, $_extension, $_before, $_limit));     } else if (is_file($_dirpath . directory_separator . $_filename)) {         if (preg_match('/' . preg_quote($_extension) . '$/', $_filename)) {             if (($_before != 0 && time() - filemtime($_dirpath . directory_separator . $_filename)) < $_before) {                 continue;             }             $_scannedfiles[] = substr($_filename, 0, -strlen($_extension) - 1);         }     }     if (count($_scannedfiles) >= $_limit) {         break;     } } closedir($_handler);  return $_scannedfiles; } 

Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -