Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, i have this code to show images from a folder. How can i sort the images? i'd like the latest added file to show firs. Thank you.


PHP
<?php
$directory = 'server/php/files/';
$thumbsdir = 'server/php/files/thumbnail';
$allowed_types=array('jpg','jpeg','gif','png');
$file_parts=array();
$ext='';
$title='';
$i=0;

$files = scandir($directory); foreach($files as $file)
{
	if($file=='.' || $file == '..') continue;
	$file_parts = explode('.',$file);
	$ext = strtolower(array_pop($file_parts));

	$title = implode('.',$file_parts);
	$title = htmlspecialchars($title);
	
	$nomargin='';
	
	if(in_array($ext,$allowed_types))
	{
		if(($i+1)%4==0) $nomargin='nomargin';
	
		echo '
		<div class="pic '.$nomargin.'" style="background:removed('.$thumbsdir.'/'.$file.') no-repeat 50% 50%; margin-removed19px;">
		<a href="'.$directory.'/'.$file.'" title="'.$title.'" target="_blank">'.$title.'</a>
		</div>';
		
		$i++;
		
	}
}

closedir($dir_handle);

?>
Posted

You can use DirectoryIterator with file's latest change time, DirectoryIterator::getCTime:
http://php.net/manual/en/class.directoryiterator.php[^],
http://php.net/manual/en/directoryiterator.getctime.php[^].

See also other time-related members:
http://php.net/manual/en/directoryiterator.getatime.php[^],
http://php.net/manual/en/directoryiterator.getmtime.php[^].

You will find code samples on the help page referenced above, the first link.

—SA
 
Share this answer
 
Try this link may be it helps

Sort pictures by date, newest first in PHP[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900