Jump to content

Recommended Posts

Hi guys,

 

I'm really stuck with this and wonder if any of you can help me.  I have the following code which reads a list of image files from a directory and then outputs the results to the page:

 

function listimages()
{
global $settings;

	$dir_handle = opendir($settings['uploadpath']);

	while ($file = readdir($dir_handle)) 
	{
		if ( !eregi( "gif|jpg|png" , $file ) )
		{
			continue;
		}
		else
		{
			list($width,$height) = getimagesize($settings['uploadpath'].$file);

			$file_size = returnFilesize(filesize($settings['uploadpath'].$file));

			$content .= '<div class="thumbnail"><img src="'.$settings['thumbpath'].''.$file.'" alt="'.$file.'"/></div>';

			$content .= '<div>';

			$content .= '<p>Filename:  '.$file.'</p>';

			$content .= '<p>Width:  '.$width.' pixels</p>';

			$content .= '<p>Height:  '.$height.' pixels</p>';

$content .= '</div>';

		}			

	}
	closedir($dir_handle);

	echo $content;	
}

 

However, I would like the above to be outputted in date order (newest shown first).  I know I need to use the "filemtime" function to achieve this but I've spent ages on it now and nothing I do seems to work.

 

Can anybody please help me?

 

Thanks a lot

You need to create an array of all the files in the directory first. At the moment you are just printing them out and there is no way you can sort the data. Make a function that creates an array of files from a given directory. The array must contain the file date. You can then sort the array by this value and return it from the function. You can then loop over the array to print to the screen.

Hi Neil,

 

Thanks for your reply, I tried this but couldn't get the filename, I was just getting the file date.  Not sure how I would go about getting the data I need out - filename and created date.

 

Do I need to create two arrays and then extract the relevant data to each array?  But then, how would I sort them both properly?

 

If you could give me a basic example to point me in the right direction it would be much appreciated.

 

Thanks

If you set the array key to be the date and set the array data to be the filename, you can just sort by the array keys

 

// in your loop
$arr[$date] = $filename; // save the date/filename

 

 

// before you iterate over the array to print the contents
krsort($arr); // sort by the date keys, high to low

Thanks for your reply PFMaBiSmAd, I'm still quite a beginner at PHP and arrays are something I'm still having a problem with.

 

Whereabouts would I stick the above code?  Sorry to be a pain, once I get an idea of where the code is meant to be and how it's written I can expand on that but at the moment I'm still totally stuck as to what to do!

 

Sorry.

 

Thanks

Create a function that you can reuse. You will pass in a directory to the function and it will return an array of all files in date order. Do some research to teach yourself. If I lay down the whole code it will mean nothing. Read on arrays and functions.

 

Complete this function and look at php.net to understand the core functions used within it

 

<?php

// return all files by date order in a given dir
function getFilesFromDir($dir) {
	$fileArray = array();
	// return empty array if not a directory
	if(!is_dir($dir)) {
		return $fileArray;
	}
	$dObj = dir($dir);
	while(false !== ($file = $dObj->read())) {
		// ignore directories
		if($file != '.' && $file != '..' && !is_dir($dir.$file)) {
			// store the file in the array
			// use the file date as the array value i.e $fileArray[image.jpg] = 20090412
			$fileArray[$file] = ;
		}
	}

	// now sort the array
	// sort code here

	// return array
	return $fileArray;

}


// run the function
$files = getFilesFromDir("/usr/local/src/php/");
// loop through files
foreach($files as $file => $date) {
	print $file." (".$date.")<br />";
}

?>

If you set the array key to be the date and set the array data to be the filename

I would set the filename as the key not the date as 2 files could have the same date however filenames are unique within a directory.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.