Jump to content

Listing level of a directory differently...


Peuplarchie

Recommended Posts

Good day to you all,

                  I'm listing a directory, folder only and recurcively.

                  I'm wondering if :

 

1) I can display the folders in level 0 of the directory bold instead of as a link.

 

 

Here is my code :

 



<?php
//Start a session
session_start();

// Error report put at report all
error_reporting(E_ALL);


ini_set("display_errors", "on");
        
set_time_limit(0);

//Put as variable the directory i start from
$directory = "Trips/";

// The dirlist function declarations
function dirList ($directory)
{

    //create 2 arrays - one for folders and one for files
   $folders = array();
   $files = array();

    // create a handler for the directory
    $handler = opendir($directory);

    // keep going until all files in directory have been read
while (false !== ($file = readdir($handler))) {  

        // if $file isn't this directory or its parent,
        // add it to the results array
        if ($file != '.' && $file != '..')
       
        // If file is directory, mark it in bold.

       if(is_dir($directory.$file)) {
        array_push($folders,$file);

	    if (time() - filemtime($directory.$file) < 604800) { 
// If file is younger than a month, write in red 
    $folder_modified[] = "<li id=\"folder\"><a href=\"index.html\" onclick=\"load('folder_view.php?dir=".$directory."".$file."','boxdisp');return false;\" style=\"color:#DB1212;\">".$file."</a>";

} elseif (time() - filemtime($directory.$file) < 31556926) { 
// If file is older than a month but younger than a year, write in green 
    $folder_modified[] = "<li id=\"folder\"><a href=\"index.html\" onclick=\"load('folder_view.php?dir=".$directory."".$file."','boxdisp');return false;\" style=\"color:#00300;\">".$file."</a>";

// file, folder, older than a year, write it in black
} else {$folder_modified[] = "<li id=\"folder\"><a href=\"index.html\" onclick=\"load('folder_view.php?dir=".$directory."".$file."','boxdisp');return false;\" style=\"color:#000000;\">".$file."</a>";}



// Here I would list the files too but I don't need it for this site.
        // Else not styled
        }else{
        array_push($files,$file);
     $filenamecor = substr($file, 0, -4);
    if (time() - filemtime($directory.$file) < 604800) {
    $file_modified[] = '<span style="color:#DB1212;">'.$filenamecor.'<span>';
} elseif (time() - filemtime($directory.$file) < 31556926) {
    $file_modified[] = '<span style="color:#003366;">'.$filenamecor.'<span>';
} else {$file_modified[] = '<span style="color:#000000;">'.$filenamecor.'<span>';}
// write nothing cause I don't need the files to be listed .

    }
    }


//declaring the variable that the result will be put in.
$output = "";
// starting the list by 
    $output .= "<ul id=\"".preg_replace('/\//','_',substr($directory,0,strlen($directory)-1))."\">\n"; //start a new unordered list for every iteration through dirList

$dircor = $directory;
    // tidy up: close the handler
    closedir($handler);
    foreach($folders as $folder=>$file) {

      $output .= "".$folder_modified[$folder]; //echo the folder name enclosed in a list item
      $output .=   dirList($directory.$file.'/'); //loop through the contents of $folder
      $output .= "\n"; //close this list item after all files and folders in $folder have been looped through
   
   
    }

    foreach($files as $key=>$file) {

    }


    $output .= "</ul>\n"; //close the unordered list

	 return $output;

echo $output;

}

$list = dirList ($directory);

 

Let me know if you need mode information.

Thanks !

Without going trough the code itself, add a second parameter $link = false;

and inside the function for the recursive path call it with true?

 

Then a simple

if( $link)
{
   echo '<a href="#">'.$dir.'</a>';
}
else
{    
   echo '<b>'.$dir.'</b>';
}

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.