Jump to content

Sort Function By Filetime


HCProfessionals

Recommended Posts

Currently I'm using the asort function to list by filename, but I'm trying to list by filetime to order from most newest file upload to oldest.

 

 function listQueue($queue, $path)
 {
  arsort($queue);
  foreach ($queue as $file)
  {
   listFile($file, $path);
  }
 }


 function listFile($file, $path)
 {
  list($width, $height, $type, $attr) = getimagesize($path.$file);
  $image_size = imageSize($path.$file);  

  echo "<a href=\"".$path.$file."\"><img src=\"getimage.php?img=".str_replace("./download/", "", $path.$file)."&w=140&h=79\" alt=\"".date("F d, Y", filemtime($path.$file))."\" title=\"Resolution: ".$width."x".$height.". Filesize: ".$image_size.". Date Uploaded: ".date("F d, Y", filemtime($path.$file)).".\" /></a>";
 }

Link to comment
Share on other sites

I would put ALL the data into a multidimensional array - then do the sort - then do the output.

 

function sortFilesByTime($a, $B)
{
   //Sort descending by time value
   return $b['time'] - $a['time'];
}

function listQueue($queue, $path)
{
   //Create array to store all file data
   $fileArray = array();

   //Put files data into a multi-dimensional array
   foreach ($queue as $file)
   {
    $full_path = $path . $file;
    list($width, $height, $type, $attr) = getimagesize($full_path);
    $fileArray[] = array(
	    'name'   => $file,
	    'path'   => $full_path,
	    'dload'  => str_replace('./download/', '', $full_path),
	    'width'  => $width,
	    'height' => $height,
	    'type'   => $type,
	    'attr'   => $attr,
	    'size'   => imageSize($full_path),
	    'time'   => filemtime($full_path),
	    'date'   => date("F d, Y", filemtime($full_path))
    );

    //Sort array using custom function
    usort($fileArray, 'sortFilesByTime');
   }

   //Create the output
   $output = '';
   foreach($fileArray as $file)
   {
    $output .= "<a href=\"{$file['path']}\">";
    $output .= "<img src=\"getimage.php?img={$file['dload']}&w=140&h=79\" alt=\"{$file['date']}\" title=\"Resolution: {$file['width']}x{$file['height']}Filesize: {$file['size']} Date Uploaded: {$file['date']}.\" />";
    $output .= "</a>";
   }

   //Return the output
   return $output;
}

//Usage
echo listQueue($arrayOfFileNames, $StringOfPath);

Link to comment
Share on other sites

First of all my signature states

I do not always test the code I provide, so there may be some syntax errors.

I expect the person that I create the code for to take the time to debug it for minor errors. I'm typically just providing code as a framework. I expect the person to read through the code I provide (especially the comments) and understand it rather than blindly copy/pasting it in. But, I am always happy to help debug more troublesome issues. When someone just states "I get this error" it tells me they spent zero time trying to figure it out for themselves.

 

But, in this instance there is absolutely nothing I can do to help. In your original code you had a function that required an input parameter called $queue which was obviously an array since it was used in a foreach() loop. In the code I provided I also used an input parameter called $queue with the expectation that it would be an array.

 

In the usage example I provided I gave you this

//Usage
echo listQueue($arrayOfFileNames, $StringOfPath);

 

I thought I named those example variables plainly enough so that I didn't have to spell out what the expectation was for their contents. Did you define $arrayOfFileNames and $StringOfPath appropriately (or use other appropriate variables of the correct type) when calling the function?

Link to comment
Share on other sites

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.