Tag Archives: directory

Displaying Ordered Directory Listings

This is a follow up to a previous post http://sheldon.lendrum.co.nz/clean-dynamic-directory-listing-with-php_206/04/ that will Dynamically display the files in a set directory.

In this version the files can be ordered by the date that they were last modified. I will also explain how to order both Ascending and Descending by the date the file was created also.

Example Usages:

< ?php
	//  EAMPLE USAGE
	//  SHOWS THE 10 NEWEST .php FILES IN A UNORDERED LIST
	echo("
    ". direcotryList("10","./",".php","
  • ","
  • \n") .""); // SHOWS ALL JPG IMAGES AS THUMBNAILS echo(direcotryList("","./images/",".jpg","\n")); ?>

< ?php

	// DIRECTORY DISPLAY USING GLOB SORTNG BY LAST MIDIFIED FILE DATE.
	// IF ALL PERIMATORS ARE EMPTY WILL SCAN DEFAULT DIRECTORY
	// '$display' =>  AMOUNT OF RECORDS TO DISPLAY
	// '$path'    =>  PATH TO DIRECOTRY TO SCAN
	// '$ext'     =>  FILES EXT,  IF YOU WANT OT ECLUDE IT,  OR ONLY SCAN FOR ONE TYPE OF FILE
	// '$start'   =>  APPEND TO START OF OUTPUT LINE EG: '
  • ' OR '
    ' // '$end' => APPEND TO END OF OUTPUT LINE EG: '
  • ' OR '\n' => DEFAULT '\n' function direcotryList($path = "./",$display = NULL, $ext = NULL, $start = NULL, $end = "\n"){ $return = (int)0; $output = ""; $ignore = array(".","..","thumbs.db"); if(empty($display)){ $display = time() * 1000; } if(($count = count($files = glob($path ."*". $ext))) > 0){ foreach($files as $v){ $filemtime[] = filemtime($v); } array_multisort($filemtime, SORT_DESC, $files); foreach($files as $file){ if($return < = $display){ if(is_file($file) and !in_array($file, $ignore)){ $output .= $start . $file . $end; $return++; } } } } return $output; } ?>

    Alterations:

    To order the directory by Creation time rather than the current Modified date Alter line 17 to filectime.

    $filemtime[] = filectime($v);

    Currently the output is orders Newest to Oldest, to reverse the Order, change the Constant in the array_multisort on line 19.

    array_multisort($filemtime, SORT_DESC, $files);

    If you have any comments or suggestions, please ask below.