Page 1 of 1

Order by date for file upload script

Posted: Wed May 17, 2006 4:24 am
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]

Posted: Wed May 17, 2006 5:29 am
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>";
}