Bricktop Posted July 13, 2009 Share Posted July 13, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/165785-sort-files-from-directory-in-date-created-order/ Share on other sites More sharing options...
JonnoTheDev Posted July 13, 2009 Share Posted July 13, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/165785-sort-files-from-directory-in-date-created-order/#findComment-874589 Share on other sites More sharing options...
Bricktop Posted July 13, 2009 Author Share Posted July 13, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/165785-sort-files-from-directory-in-date-created-order/#findComment-874615 Share on other sites More sharing options...
PFMaBiSmAd Posted July 13, 2009 Share Posted July 13, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/165785-sort-files-from-directory-in-date-created-order/#findComment-874629 Share on other sites More sharing options...
Bricktop Posted July 13, 2009 Author Share Posted July 13, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/165785-sort-files-from-directory-in-date-created-order/#findComment-874677 Share on other sites More sharing options...
JonnoTheDev Posted July 14, 2009 Share Posted July 14, 2009 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 />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/165785-sort-files-from-directory-in-date-created-order/#findComment-875039 Share on other sites More sharing options...
JonnoTheDev Posted July 14, 2009 Share Posted July 14, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/165785-sort-files-from-directory-in-date-created-order/#findComment-875040 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.