Log in

View Full Version : Reading the directory structure



SameerMirza
03-30-2009, 02:46 PM
Hi all,

I have used the below script to read the directory struture in order to build the tree menu for my web.



<?php
$sub = ($_GET['dir']);
$path = 'enter/your/directory/here/';
$path = $path . "$sub";
$dh = opendir($path);
$i=1;
while (($file = readdir($dh)) !== false) {
if($file != "." && $file != "..") {
if (substr($file, -4, -3) =="."){
echo "$i. $file <br />";
}else{
echo "$i. <a href='?dir=$sub/$file'>$file</a><br />";
}
$i++;
}
}
closedir($dh);
?>


This script read the directory structure in alpthabatical order but my requirement is to read on the basis of date created. It will be great if any one could tell me how to achieve it.

Regards,

Sameer.

Master_script_maker
03-31-2009, 12:18 AM
Google is your friend: http://www.google.com/search?hl=en&q=php+directory+sort+by+date&btnG=Google+Search&aq=f&oq=

SameerMirza
04-07-2009, 04:49 PM
hi there,

I am using the below code while trying to access the directory structure. The code I took from the link above is,


function LoadFiles($dir,$filter="")
{
$Files = array();
$It = opendir($dir);
if (! $It)
die('Cannot list files for ' . $dir);
while ($Filename = readdir($It))
{
if ($Filename != '.' && $Filename != '..' )
{
if(is_dir($dir . $Filename))
{
$Files = array_merge($Files, LoadFiles($dir . $Filename.'/'));
}
else
if ($filter=="" || preg_match( $filter, $Filename ) )
{
$LastModified = filemtime($dir . $Filename);
$Files[] = array($dir .$Filename, $LastModified);
}

else
continue;

}
}
return $Files;
}
function DateCmp($a, $b)
{
return strnatcasecmp($a[1] , $b[1]) ;
}

function SortByDate(&$Files)
{
usort($Files, 'DateCmp');
}

$Files = LoadFiles("../../templates/images/data/communes/");
SortByDate($Files);
reset($Files);
while (list($k,$v) =each($Files))
{
?> - <?=$v[0]?> <?= date('Ymd h:i',$v[1])?></li><?
}


where as I am just passing the following $path variable to funcation LoadFiles().

$sub = ($_GET['dir']);
$path = 'C:\Inetpub\wwwroot\reports\Backup\Backup Usage\';
$path = $path."$sub";

$Files = LoadFiles($path);
(i also tried plan path i.e $path = 'C:\Inetpub\wwwroot\reports\Backup\Backup Usage\';)

But the program is giving me 'unexpected T_string error'. Does any one got an idea what this error would be reffering to ?

I will be great if some one could solve this, as it is realy urgent for me.



Regards,

Sameer.