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
Share on other sites

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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.