Order by date for file upload script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
teajay_99
Forum Newbie
Posts: 1
Joined: Wed May 17, 2006 4:11 am

Order by date for file upload script

Post by teajay_99 »

Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I'm fairly new to PHP but have managed to cobble together something that works quite well as a file upload for a client. However I am trying to modify this so the files upload in date order so the most recent are at the top of the list. 

I know it must be fairly easy to do but cannot figure out a way of doing it yet, If anyone could help with this, I would be very grateful.


Here is my code:

Code: Select all

$handle=opendir($upload_dir);
$filelist = "";
while ($file = readdir($handle)) {
   if(!is_dir($file) && !is_link($file)) {
      $filelist .= "<a href='$upload_dir$file'>".$file."</a>";
      $filelist .= "<a href='?del=$upload_dir$file' title='delete'>x</a>";
      $filelist .= "<sub><small><span style='color:grey'> ".date("d-m H:i", filemtime($upload_dir.$file))
                   ."</span></small></sub>";
      $filelist .="<br>";
   }
}

function do_upload($upload_dir, $upload_url) {

	$temp_name = $_FILES['userfile']['tmp_name'];
	$file_name = $_FILES['userfile']['name']; 
  $file_name = str_replace("\\","",$file_name);
  $file_name = str_replace("'","",$file_name);
	$file_path = $upload_dir.$file_name;
	## ADDITION VARS
	$ToMail = "email@yahoo.co.uk";       ## YOUR EMAIL ADDRESS
    $subject = "IMAGE UPLOAD FOR ". $_POST["address"]."";
    $text = "www.website.com/subfolder".$file_name." HAS BEEN UPLOADED FOR ". $_POST["address"]."";
	## STRIP OUT THE UNWANTED CHARACTERS INCASE ANY APPEAR
	$subject = stripslashes($subject);
    $text = stripslashes($text);
	@mail("$ToMail", $subject, $text, "From: email@website.com");

	//File Name Check
  if ( $file_name =="") { 
  	$message = "Invalid File Name Specified";
  	return $message;
  }

  $result  =  move_uploaded_file($temp_name, $file_path);
  if (!chmod($file_path,0777))
   	$message = "change permission to 777 failed.";
  else
    $message = ($result)?"$file_name uploaded successfully - thank you.":
     	      "Somthing is wrong with uploading a file.";
   return $message;
}

Many Thanks,

Tracey


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by teajay_99 on Wed May 17, 2006 7:37 am, edited 1 time in total.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

You cant actually upload the files in date order, but you can certainly list the files once uploaded in date order

Replace the file listing code with something like this (Note: this is untested)

Code: Select all

while ($file = readdir($handle)) {
   if(!is_dir($file) && !is_link($file)) {
		$directory[$file] = filemtime($upload_dir.$file)
   }
}

$directory = asort($directory);

foreach($directory as $k=>$v)
{
      $filelist .= "<a href='$upload_dir$file'>".$k."</a>";
      $filelist .= "<a href='?del=$upload_dir$file' title='delete'>x</a>";
      $filelist .= "<sub><small><span style='color:grey'> ".date("d-m H:i", filemtime($v))
                   ."</span></small></sub>";
      $filelist .="<br>";
}
Post Reply