Jump to content

[SOLVED] List Dir and Files in sub Dir


Daleeburg

Recommended Posts

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

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 :-)

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('..');
?>

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

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.