Daleeburg Posted May 17, 2007 Share Posted May 17, 2007 I am trying to make a script that list all of the files in a directory and then if the file in the directory is a sub Directory it will list the files in the subdirectory. If anybody would have a clue how to do this or where to start, please tell. Also in the end i want it to check file dates and delete them if they are older then 3 days, but i dont have a clue where to start on this. thx ~D Link to comment https://forums.phpfreaks.com/topic/51879-solved-list-dir-and-files-in-sub-dir/ Share on other sites More sharing options...
phast1 Posted May 17, 2007 Share Posted May 17, 2007 You would need to do the initial file list and then use the is_dir() function to determine if an entry is a directory or not.. If it is a directory, then you basically loop back through and do the same process with the new path name.. Link to comment https://forums.phpfreaks.com/topic/51879-solved-list-dir-and-files-in-sub-dir/#findComment-255702 Share on other sites More sharing options...
phast1 Posted May 17, 2007 Share Posted May 17, 2007 Oh, you would also use the filemtime() function to determine the last modification time of a file.. Link to comment https://forums.phpfreaks.com/topic/51879-solved-list-dir-and-files-in-sub-dir/#findComment-255705 Share on other sites More sharing options...
taith Posted May 17, 2007 Share Posted May 17, 2007 not tested... but i think it'll work... lol <?php $out=array(); function get_all($dir){ $files=glob($dir); if(empty($files)) return array(); foreach($files as $file){ if(is_dir($file)) $out=array_merge($out,get_all($file.'/*')); else $out[]=$file; } return $out; } print_r(get_all('../divinelive/*')); ?> sorry... edited... now it works... lol then you just do a foreach(){} to grab the filetime(), and remove accordingly :-) Link to comment https://forums.phpfreaks.com/topic/51879-solved-list-dir-and-files-in-sub-dir/#findComment-255706 Share on other sites More sharing options...
Barand Posted May 17, 2007 Share Posted May 17, 2007 try <?php function list_files($dir, $level=0){ if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if ($file=='.' || $file=='..') continue; if (is_dir("$dir/$file")) { echo str_repeat(' ', $level*5)."<b>$file (dir)</b><br>"; list_files("$dir/$file", $level+1); } else echo str_repeat(' ', $level*5)."$file<br>"; } closedir($dh); } } list_files('..'); ?> Link to comment https://forums.phpfreaks.com/topic/51879-solved-list-dir-and-files-in-sub-dir/#findComment-255861 Share on other sites More sharing options...
Daleeburg Posted May 17, 2007 Author Share Posted May 17, 2007 once i learned that php could read files and the such i spent some time on it and came out with this working script. <?php FUNCTION vdir($dir){ $handle=opendir($dir); While (($file = readdir($handle))!==false) { If(strlen($file) >= 3){ Echo "<b>$file</b><br>"; $handle2=opendir($dir."/".$file."/"); While (($file2 = readdir($handle2))!==false) { If(strlen($file2) >= 3){ Echo " <a href=\"$dir/$file/$file2\">$file2</a><br>"; echo " last modified: " . date ("F d Y H:i:s.", filemtime($dir."/".$file."/".$file2))."<br>"; }; }; closedir($handle2); }; }; closedir($handle); } echo vdir("whatever/"); ?> Basicly it does 2 levels of folders, so if you have all folders in the top level and all files in the next it will do it. The sting limit makes it so that you dont get the (.) and (..), but all of the folders have to be over 2 characters long. Also the main file name is bold, you can link to the individual files, and veiw the creation date. ~D Link to comment https://forums.phpfreaks.com/topic/51879-solved-list-dir-and-files-in-sub-dir/#findComment-255869 Share on other sites More sharing options...
taith Posted May 17, 2007 Share Posted May 17, 2007 mine does all levels of files ;-) Link to comment https://forums.phpfreaks.com/topic/51879-solved-list-dir-and-files-in-sub-dir/#findComment-255957 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.