Jump to content
xisto Community
Sign in to follow this  
itssami

Displaying Latest Added Files Of Subdirectories ?

Recommended Posts

For example,i have a directory , which has 3 sub directories a,b and c...and i have some files in all subdirectories..is it possible that i can display the latest added files to any of sub directory a,b, or c.?i think i will have to use sort by date function but how it should be done that it compares the files of all the subdirectories directories ?

Edited by itssami (see edit history)

Share this post


Link to post
Share on other sites

For example,i have a directory , which has 3 sub directories a,b and c...and i have some files in all subdirectories..is it possible that i can display the latest added files to any of sub directory a,b, or c.?i think i will have to use sort by date function but how it should be done that it compares the files of all the subdirectories directories ?



add this to the previous script, it doesn't check sub dirs but you can do sub dirs one at a time,

else if($file != '.' && $file != '..') {$narray[$i]=$file;$i++;//check for a / at the end of the path add one if not thereif(substr($path, -1)!='/') {		$path .= '/';	} if(filemtime($path.$file)>$n) {		$n = filemtime($path.$file);		$newestname = $file;			} 		   }}rsort($narray);for($i=0; $i<sizeof($narray); $i++){echo "<a href=".chr(34).$path.$narray[$i].chr(34).">".$narray[$i]."</a><br/>";}echo $newestname;

Share this post


Link to post
Share on other sites

Hey..I think this doesnot display the entries by date.. i want that the latest added file in any directory should appear at upper..and then others lower by date of adding..the following function displays the files by alphabetically.. <_<:lol:

add this to the previous script, it doesn't check sub dirs but you can do sub dirs one at a time,

else if($file != '.' && $file != '..') {$narray[$i]=$file;$i++;//check for a / at the end of the path add one if not thereif(substr($path, -1)!='/') {		$path .= '/';	} if(filemtime($path.$file)>$n) {		$n = filemtime($path.$file);		$newestname = $file;			} 		   }}rsort($narray);for($i=0; $i<sizeof($narray); $i++){echo "<a href=".chr(34).$path.$narray[$i].chr(34).">".$narray[$i]."</a><br/>";}echo $newestname;

Share this post


Link to post
Share on other sites

Try something like this. It's not perfect, and I only wrote and tested it very quickly, but it's a start:

<table width="100%"><tr><td width="90%"><strong>File</strong></td><td width="10%"><strong>Last Modified</strong></td></tr><?php$files = array();$directories = array('.');for( $i=0;$i<count($directories);$i++ ) {  $path = $directories[$i];  $directory = opendir($path);  while( $contents = readdir($directory) ) {	if( $contents != '.' && $contents != '..' ) {	  $item = $path . '/' . $contents;	  if( is_dir($item) && !in_array($item, $directories) ) {		$directories[] = $item;	  } else {		//		// Note that if you wanted to sort the files by the time		// they were created rather than modified, you would use		// the function filectime() rather than filemtime() here.		//		$filetime = filemtime($item);		while( isset($files[$filetime]) ) {		  $filetime++;		}		$files[$filetime] = $item;	  }	}  }  closedir($directory);}krsort($files);$files_k = array_keys($files);for( $i=0;$i<count($files);$i++ ) {  echo '<tr>' . "\n";  $filetime = $files_k[$i];  $file = $files[$filetime];  echo '<td style="font-size:10px;">' . $file . '</td>' . "\n";  echo '<td style="font-size:10px;">' . date('d-M-Y H:i', $filetime) . '</td>' . "\n";  echo '</tr>' . "\n";}?></table>

Share this post


Link to post
Share on other sites

It looks like someone already answered you, but here's an alternative implimentation that allows you to specify the folders you want sorted. I'm not sure which one you'll prefer.

<?php	function dirList($dir) 	{		$results = array();		$handle = opendir($dir);			while($file = readdir($handle)) 			if ($file != '.' && $file != '..')				$results[] = array($file, filemtime($dir . $file));			closedir($handle);		return($results);		}		function reorderByDate($first, $second)	{		return($first[1] < $second[1]);	}	/* Please only change the following line of code */	/* Directory names *must* end with a "/" */	$listOfDirs = array("folder1/", "folder2/");	$results = array();		foreach($listOfDirs as $dir)		$results = array_merge($results, dirList($dir));			usort($results, "reorderByDate");		/* Displays the files -- change this if you want them to display differently */	foreach($results as $file)		echo $file[0] . "<br />\n";   ?>

Share this post


Link to post
Share on other sites

Thank you very much for both of you guys to help me.. both of them works... but they just display the file names..and date..i want to give the hyperlink to the file also , so that when some one clicks on any of the file , it opens that file directly..(these functions just prints the names of the files.)
thank you once again

It looks like someone already answered you, but here's an alternative implimentation that allows you to specify the folders you want sorted. I'm not sure which one you'll prefer.

<?php	function dirList($dir) 	{		$results = array();		$handle = opendir($dir);			while($file = readdir($handle)) 			if ($file != '.' && $file != '..')				$results[] = array($file, filemtime($dir . $file));			closedir($handle);		return($results);		}		function reorderByDate($first, $second)	{		return($first[1] < $second[1]);	}	/* Please only change the following line of code */	/* Directory names *must* end with a "/" */	$listOfDirs = array("folder1/", "folder2/");	$results = array();		foreach($listOfDirs as $dir)		$results = array_merge($results, dirList($dir));			usort($results, "reorderByDate");		/* Displays the files -- change this if you want them to display differently */	foreach($results as $file)		echo $file[0] . "<br />\n";   ?>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.