Page 1 of 1

help with the subdir listing and sorting by date

Posted: Sun Mar 28, 2010 12:08 pm
by bd_safa
I am trying to write a code to list the all subdir of the main path directories and also sort them by date ... i search the net and came up with this code :

Code: Select all

<?
$t = 0;
$f = 0;
$dir_name = array();
$dir_time = array();
$pathc = "$pathc";
$dir_handle = @opendir($pathc) or die("Unable to open $pathc");
while ($list = readdir($dir_handle)) {
	    if ($list != "." && $list != ".." && $list != "index.php~") { 
		while ($list = readdir($dir_handle)) {
			$path = "$pathc/$list";
				if (@$handle = opendir($path)) {
					while ($file = readdir($handle)) {
						if($file != "." && $file != ".." && $file != "index.php~") {
						$fName = $file;
						$files = $path.'/'.$file; 
							if(false== is_file($file)){ 
							$dir_time[$t++] = filemtime($files);
							$dir_name[$f++] = $file;
							}
						}
					}
					closedir($handle);
				}
		}
		closedir($dir_handle);
		}
asort( $dir_time);
asort( $dir_name);
}
foreach ($dir_time as $key=>$ftime){
$fname = $dir_name[$key];
echo $fname.'<br/>';
}?> 
its doing the job perfectly, but there is 2 major problems :
1- it also lists the main directories ($Pathc directories) :banghead: .
2- the current sorting is descending, i can't change it to ascending.

also if possible i want to link the list items to $path/$file...

thanks in advance :)

Re: help with the subdir listing and sorting by date

Posted: Mon Mar 29, 2010 5:08 am
by learnerabn
<?
$t = 0;
$f = 0;
$dir_name = array();
$dir_time = array();
$pathc = "$pathc";
$dir_handle = @opendir($pathc) or die("Unable to open $pathc");
while ($list = readdir($dir_handle)) {
if ($list != "." && $list != ".." && $list != "index.php~") {
while ($list = readdir($dir_handle)) {
$path = "$pathc/$list";
if (@$handle = opendir($path)) {
while ($file = readdir($handle)) {
if($file != "." && $file != ".." && $file != "index.php~") {
$fName = $file;
$files = $path.'/'.$file;
if(false== is_file($file)){
$dir_time[$t++] = filemtime($files);
$dir_name[$f++] = $file;
}
}
}
closedir($handle);
}
}
closedir($dir_handle);
}
asort( $dir_time);
asort( $dir_name);
}
foreach ($dir_time as $key=>$ftime){
$fname = $dir_name[$key];
echo $fname.'<br/>';
}?>
1.to avoid the $pathc including in the list change the first if condition as
if ($list != "." && $list != ".." && $list != "index.php~" && $list != "$pathc")
2.to sort in ascending order try sort() instead of asort() or refer www.php.net for some other functions.

Re: help with the subdir listing and sorting by date

Posted: Mon Mar 29, 2010 6:35 am
by bd_safa
learnerabn wrote: 1.to avoid the $pathc including in the list change the first if condition as
if ($list != "." && $list != ".." && $list != "index.php~" && $list != "$pathc")
2.to sort in ascending order try sort() instead of asort() or refer http://www.php.net for some other functions.
Thanks for your reply ...
made the changes , but nothing happened ... it still includes the $pathc directories ... :banghead:

Re: help with the subdir listing and sorting by date

Posted: Mon Mar 29, 2010 12:15 pm
by AbraCadaver
I prefer glob():

Code: Select all

foreach (glob("*", GLOB_ONLYDIR) as $dir) {
    $dirs[$dir] = filemtime($dir);
}
asort($dirs);