How can i sort the images?

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. The files are as now sorted by name. Thank you.

<?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:url('.$thumbsdir.'/'.$file.') no-repeat 50% 50%; margin-left:19px;">
		<a href="'.$directory.'/'.$file.'" title="'.$title.'" target="_blank">'.$title.'</a>
		</div>';
		
		$i++;
		
	}
}

closedir($dir_handle);

?>

I seem to remember reading somewhere that you can not get the latest created/uploaded details from Linux.

To be certain I rename my files on upload to have the date in the name; you are sure to be able to get the latest uploaded file then.

Thank’s, but i think it is possible to get latest created/uploaded details from Linux. I’m just not sure how to add the function to this code.

I’m on Windows so I don’t know how Linux would do it, but if you explain how Linux gets the created/modified time using that in a function should be easy enough.

PHP’s filemtime() function does just this. It gets the file’s creation time or its last modified time (depending upon whether the file has been modified since its creation) as a unix timestamp. Here’s a simple script I hacked together to demonstrate how to grab all files from a directory and list its last modified/creation date from newest to oldest:


&lt;?php
$fileNames = glob('*.*');
$files = array();

foreach($fileNames as $file)
	$files[$file] = filemtime($file);

arsort($files);

foreach($files as $fileName =&gt; $fileModified)
	echo "File: '$fileName' modified at: ", date('Y-m-d H:i:s', $fileModified), '&lt;br /&gt;';

Thank you, but how can i get the filemtime to work with my code?

I believe the following should work:


&lt;?php

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

foreach($allowed_types as $ext)
	$fileNames = array_merge($fileNames, glob("{$directory}*.{$ext}"));

foreach($fileNames as $file) {
	$f = explode('/', $file);
	$fileName = end($f);
	$files[$fileName] = filemtime($file);
}

arsort($files);

foreach(array_keys($files) as $file)
{
	$file_parts = explode('.',$file);
	$ext = strtolower(array_pop($file_parts));

	$title = implode('.',$file_parts);
	$title = htmlspecialchars($title);
	
	$nomargin = '';

	if(($i + 1) % 4 == 0)
		$nomargin = 'nomargin';

	echo '
	&lt;div class="pic '.$nomargin.'" style="background:url('.$thumbsdir.'/'.$file.') no-repeat 50% 50%; margin-left:19px;"&gt;
	&lt;a href="'.$directory.'/'.$file.'" title="'.$title.'" target="_blank"&gt;'.$title.'&lt;/a&gt;
	&lt;/div&gt;';
	
	$i++;
}

Hello, and thank you for your reply, but this code is not working, i get the following error messages:

Warning: array_merge(): Argument #2 is not an array

Warning: array_merge(): Argument #1 is not an array

Warning: Invalid argument supplied for foreach()

Could you please replace the following two lines:


foreach($allowed_types as $ext)
    $fileNames = array_merge($fileNames, glob("{$directory}*.{$ext}"));

with these lines:


$toMatch = "{$directory}*.{".implode(',', $allowed_types).'}';
$fileNames = glob($toMatch, GLOB_NOSORT | GLOB_BRACE);

and tell me whether that works?

Hello, This works perfect now. Thank you so much for your help.