Page 1 of 1

How do I sort files in date order??

Posted: Tue Jul 05, 2005 10:08 am
by andylyon87
How do I sort a number of files so they are in date order with the most recently added last.

and how would I fit it into this code:

Code: Select all

<?print('<TABLE cellpadding=0 cellspacing=4 border=0 width=760>');
$dir = opendir("$id/");
$cols = 0;
while ($file = readdir($dir)) {
  if (!$cols){ print('<tr>');}
  if(is_file("$id/$file")){
  list($width, $height)=getimagesize("$id/$file");
  if($height<$width){
  print("<TD><A href=\"javascript:largeImage('$id/$file','',$width,$height)\"><img src=$id/$file alt=$file width=120 border=0></a></TD>");
  }
  if($height>=$width){
  print("<TD><A href=\"javascript:largeImage('$id/$file','',$width,$height)\"><img src=$id/$file alt=$file width=80 border=0></a></TD>");
  }
  if ($cols > 2) {
    print('</tr>');
    $cols = 0;
  }else{
  	$cols++;
  }}
}
print('</table>');
?>
Andy

Posted: Tue Jul 05, 2005 10:21 am
by djot
read filenames and filetimes into an array, sort the array, output the array.
How?
->http://de2.php.net/manual/en/ref.filesystem.php
->http://de2.php.net/manual/en/ref.array.php

Posted: Tue Jul 05, 2005 10:22 am
by pickle
There are two possible ways:

1) If you're on Linux, try calling `ls` with some additional arguments. You may be able to get it to output the list of files in chronological order. Then, just sort through that output.

2) After retrieving a file listing the way you currently are, call filemtime() to get the last modified time of each file. You can then sort based on that time.

Posted: Tue Jul 05, 2005 10:24 am
by andylyon87
If I use filemtime() to get the last modification date which would be the upload date how would i sort that into an array and use that code because the problem I have is then projecting the array into a table of x columns.

Posted: Tue Jul 05, 2005 10:27 am
by pickle
If you've got an array of filenames, make a new array using the file names as the key, and the modified time as the value. Calling asort() will then sort by array value, while keeping the key=>value pairs associated.

Posted: Tue Jul 05, 2005 10:36 am
by djot
The upload date would be the date of creation of upload file. Don't know how you can access this. filectime, fileatime, filemtime, stats all do not fit.

Posted: Tue Jul 05, 2005 10:58 am
by pickle
When a file is uploaded though, it's also modified. fileatime() would also work would it not?

Posted: Tue Jul 05, 2005 11:58 am
by andylyon87
thanks for the help will have to try it, will be right back if it doesnt work but I can see what I need to do now.

Posted: Tue Jul 05, 2005 11:59 am
by djot
-
If he wants to keep the date of upload, fileatime won't fit if the file is altered in some way later (edited resized, redampled, etc.), because then modified time would be different from upload time.

Perhaps store such information as upload date, original filename, etc. in a database.

djot
-

Posted: Wed Jul 06, 2005 11:59 am
by andylyon87
Im sorry guys but I sill dont get it

I jus get errors and I have no idea of how I go about adaptin the php.net sample code. If someone could tell me I would be very greatful. I have the thing so it shows the files but it doesnt sort them. I think I have put each file into its own array, how do I prevent this.

Code: Select all

<?print('<TABLE cellpadding=0 cellspacing=4 border=0 width=760>');
$dir = opendir("$id/");
$cols = 0;
while ($file = readdir($dir)) {  //first while
	$time=filemtime(&quote;$id/$file&quote;);  // gets filetime
	$times=array(&quote;$time&quote;=&gt;&quote;$id/$file&quote;);  //puts into an array
	asort($times);  //sorts array
	reset($times); // resets array
	while (list($key, $val) = each($times)) { //lists the array
   if (!$cols){ print('&lt;tr&gt;');}  //from here it works fine
  if(is_file(&quote;$id/$file&quote;)){
  list($width, $height)=getimagesize(&quote;$id/$file&quote;);
  if($height&lt;$width){
  print(&quote;&lt;TD&gt;&lt;A href=\&quote;javascript:largeImage('$id/$file','',$width,$height)\&quote;&gt;&lt;img src=$id/$file alt=$file width=120 border=0&gt;&lt;/a&gt;&lt;/TD&gt;&quote;);
  }
  if($height&gt;=$width){
  print(&quote;&lt;TD&gt;&lt;A href=\&quote;javascript:largeImageaptin the php.net sample code. If someone could tell me I would be very greatful. I have the thing so it shows the files but it doesnt sort them. I think I have put each file into its own array, how do I prevent this.

Code: Select all

<?print('<TABLE cellpadding=0 cellspacing=4 border=0 width=760&gt;');
$dir = opendir(&quote;$id/&quote;);
$cols = 0;
while ($file = readdir($dir)) {  //first while
	$time=filemtime(&quote;$id/$file&quote;);  // gets filetime
	$times=array(&quote;$time&quote;=&gt;&quote;$id/$file&quote;);  //puts into an array
	asort($times);  //sorts array
	reset($times); // resets array
	while (list($key, $val) = each($times)) { //lists the array
   if (!$cols){ print('&lt;tr&gt;');}  //from here it works fine
  if(is_file(&quote;$id/$file&quote;)){
  list($width, $height)=getimagesize(&quote;$id/$file&quote;);
  if($height&lt;$width){
  print(&quote;&lt;TD&gt;&lt;A href=\&quote;javascript:largeImage('$id/$file','',$width,$height)\&quote;&gt;&lt;img src=$id/$file alt=$file width=120 border=0&gt;&lt;/a&gt;&lt;/TD&gt;&quote;);
  }
  if($height&gt;=$width){
  print(&quote;&lt;TD&gt;&lt;A href=\&quote;javascript:largeImage('$id/$file','',$width,$height)\&quote;&gt;&lt;img src=$id/$file alt=$file width=80 border=0&gt;&lt;/a&gt;&lt;/TD&gt;&quote;);
  }
  if ($cols &gt; 2) {
    print('&lt;/tr&gt;');
   I prevent this.

Code: Select all

<?print('<TABLE cellpadding=0 cellspacing=4 border=0 width=760>');
$dir = opendir(&quote;$id/&quote;);
$cols = 0;
while ($file = readdir($dir)) {  //first while
	$time=filemtime(&quote;$id/$file&quote;);  // gets filetime
	$times=array(&quote;$time&quote;=&gt;&quote;$id/$file&quote;);  //puts into an array
	asort($times);  //sorts array
	reset($times); // resets array
	while (list($key, $val) = each($times)) { //lists the array
   if (!$cols){ print('&lt;tr&gt;');}  //from here it works fine
  if(is_file(&quote;$id/$file&quote;)){
  list($width, $height)=getimagesize(&quote;$id/$file&quote;);
  if($height&lt;$width){
  print(&quote;&lt;TD&gt;&lt;A href=\&quote;javascript:largeImage('$id/$file','',$width,$height)\&quote;&gt;&lt;img src=$id/$file alt=$file width=120 border=0&gt;&lt;/a&gt;&lt;/TD&gt;&quote;);
  }
  if($height&gt;=$width){
  print(&quote;&lt;TD&gt;&lt;A href=\&quote;javascript:largeImage('$id/$file','',$width,$height)\&quote;&gt;&lt;img src=$id/$file ne could tell me I would be very greatful. I have the thing so it shows the files but it doesnt sort them. I think I have put each file into its own array, how do I prevent this.

Code: Select all

&lt;?print('&lt;TABLE cellpadding=0 cellspacing=4 border=0 width=760>');
$dir = opendir("$id/&quote;);
$cols = 0;
while ($file = readdir($dir)) {  //first while
	$time=filemtime(&quote;$id/$file&quote;);  // gets filetime
	$times=array(&quote;$time&quote;=&gt;&quote;$id/$file&quote;);  //puts into an array
	asort($times);  //sorts array
	reset($times); // resets array
	while (list($key, $val) = each($times)) { //lists the array
   if (!$cols){ print('&lt;tr&gt;');}  //from here it works fine
  if(is_file(&quote;$id/$file&quote;)){
  list($width, $height)=getimagesize(&quote;$id/$file&quote;);
  if($height&lt;$width){
  print(&quote;&lt;TD&gt;&lt;A href=\&quote;javascript:largeImage('$id/$file','',$width,$height)\&quote;&gt;&lt;img src=$id/$file alt=$file width=120 border=0&gt;&lt;/a&gt;&lt;/TD&gt;&quote;);
  }
  if($height&gt;=$width){
  print(&quote;&lt;TD&gt;&lt;A href=\&quote;jas and I have no idea of how I go about adaptin the php.net sample code. If someone could tell me I would be very greatful. I have the thing so it shows the files but it doesnt sort them. I think I have put each file into its own array, how do I prevent this.

Code: Select all

&lt;?print('&lt;TABLE cellpadding=0 cellspacing=4 border=0 width=760&gt;');
$dir = opendir(&quote;$id/&quote;);
$cols = 0;
while ($file = readdir($dir)) {  //first while
	$time=filemtime(&quote;$id/$file&quote;);  // gets filetime
	$times=array(&quote;$time&quote;=&gt;&quote;$id/$file&quote;);  //puts into an array
	asort($times);  //sorts array
	reset($times); // resets array
	while (list($key, $val) = each($times)) { //lists the array
   if (!$cols){ print('&lt;tr&gt;');}  //from here it works fine
  if(is_file(&quote;$id/$file&quote;)){
  list($width, $height)=getimagesize(&quote;$id/$file&quote;);
  if($height&lt;$width){
  print(&quote;&lt;TD&gt;&lt;A href=\&quote;javascript:largeImage('$id/$file','',$width,$height)\&quote;&gt;&lt;img src=$id/$file alt=$file width=120 border=0&gt;&lt;/a&gt;&lt;/TD&gt;&quote;);
  }
  if($height&gt;=$width){
  print(&quote;&lt;TD&gt;&lt;A href=\&quote;javascript:largeImage('$id/$file','',$width,$height)\&quote;&gt;&lt;img src=$id/$file alI sill dont get it

I jus get errors and I have no idea of how I go about adaptin the php.net sample code. If someone could tell me I would be very greatful. I have the thing so it shows the files but it doesnt sort them. I think I have put each file into its own array, how do I prevent this.

Code: Select all

&lt;?print('&lt;TABLE cellpadding=0 cellspacing=4 border=0 width=760&gt;');
$dir = opendir(&quote;$id/&quote;);
$cols = 0;
while ($file = readdir($dir)) {  //first while
	$time=filemtime(&quote;$id/$file&quote;);  // gets filetime
	$times=array(&quote;$time&quote;=&gt;&quote;$id/$file&quote;);  //puts into an array
	asort($times);  //sorts array
	reset($times); // resets array
	while (list($key, $val) = each($times)) { //lists the array
   if (!$cols){ print('&lt;tr&gt;');}  //from here it works fine
  if(is_file(&quote;$id/$file&quote;)){
  list($width, $height)=getimagesize(&quote;$id/$file&quote;);
  if($height&lt;$width){
  print(&quote;&lt;TD&gt;&lt;A href=\&quote;javascript:largeImage('$id/$file','',$width,$height)\&quote;&gt;&lt;img src=$id/$file alt=$file width=120 border=0&gt;&lt;/a&gt;&lt;/TD&gt;&quote;);
  }
  if($height&gt;=$width){
  print(&quote;&lt;TD&gt;&lt;A href=\&quote;javascript:largeImage('$id/$file','',$width,$height)\&quote;&gt;&lt;img src=$id/$file alt=$file width=80 border=0&gt;&amI sill dont get it

I jus get errors and I have no idea of how I go about adaptin the php.net sample code. If someone could tell me I would be very greatful. I have the thing so it shows the files but it doesnt sort them. I think I have put each file into its own array, how do I prevent this.

Code: Select all

&lt;?print('&lt;TABLE cellpadding=0 cellspacing=4 border=0 width=760&gt;');
$dir = opendir(&quote;$id/&quote;);
$cols = 0;
while ($file = readdir($dir)) {  //first while
	$time=filemtime(&quote;$id/$file&quote;);  // gets filetime
	$times=array(&quote;$time&quote;=&gt;&quote;$id/$file&quote;);  //puts into an array
	asort($times);  //sorts array
	reset($times); // resets array
	while (list($key, $val) = each($times)) { //lists the array
   if (!$cols){ print('&lt;tr&gt;');}  //from here it works fine
  if(is_file(&quote;$id/$file&quote;)){
  list($width, $height)=getimagesize(&quote;$id/$file&quote;);
  if($height&lt;$width){
  print(&quote;&lt;TD&gt;&lt;A href=\&quote;javascript:largeImage('$id/$file','',$width,$height)\&quote;&gt;&lt;img src=$id/$file alt=$file width=120 border=0&gt;&lt;/a&gt;&lt;/TD&gt;&quote;);
  }
  if($height&gt;=$width){
  print(&quote;&lt;TD&gt;&lt;A href=\&quote;javascript:largeImage('$id/$file','',$width,$height)\&quote;&gt;&lt;img src=$id/$file alt=$file width=80 border=0&gt;&lt;/a&gt;&lt;/TD&gt;&quote.net sample code. If someone could tell me I would be very greatful. I have the thing so it shows the files but it doesnt sort them. I think I have put each file into its own array, how do I prevent this.

Code: Select all

<?print('<TABLE cellpadding=0 cellspacing=4 border=0 width=760>');
$dir = opendir("$id/");
$cols = 0;
while ($file = readdir($dir)) {  //first while
	$time=filemtime("$id/$file");  // gets filetime
	$times=array("$time"=>"$id/$file");  //puts into an array
	asort($times);  //sorts array
	reset($times); // resets array
	while (list($key, $val) = each($times)) { //lists the array
   if (!$cols){ print('<tr>');}  //from here it works fine
  if(is_file("$id/$file")){
  list($width, $height)=getimagesize("$id/$file");
  if($height<$width){
  print("<TD><A href=\"javascript:largeImage('$id/$file','',$width,$height)\"><img src=$id/$file alt=$file width=120 border=0></a></TD>");
  }
  if($height>=$width){
  print("<TD><A href=\"javascript:largeImage('$id/$file','',e could tell me I would be very greatful. I have the thing so it shows the files but it doesnt sort them. I think I have put each file into its own array, how do I prevent this.

Code: Select all

<?print('<TABLE cellpadding=0 cellspacing=4 border=0 width=760>');
$dir = opendir("$id/&quote;);
$cols = 0;
while ($file = readdir($dir)) {  //first while
	$time=filemtime(&quote;$id/$file&quote;);  // gets filetime
	$times=array(&quote;$time&quote;=&gt;&quote;$id/$file&quote;);  //puts into an array
	asort($times);  //sorts array
	reset($times); // resets array
	while (list($key, $val) = each($times)) { //lists the array
   if (!$cols){ print('&lt;tr&gt;');}  //from here it works fine
  if(is_file(&quote;$id/$file&quote;)){
  list($width, $height)=getimagesize(&quote;$id/$file&quote;);
  if($height&lt;$width){
  print(&quote;&lt;TD&gt;&lt;A href=\&quote;javascript:largeImage('$id/$file','',$width,$height)\&quote;&gt;&lt;img src=$id/$file alt=$file width=120 border=0&gt;&lt;/a&gt;&lt;/TD&gt;&quote;);
  }
  if($height&gt;=$width){
  print(&quote;&lt;TD&gt;&lt;A href=\&q

I jus get errors and I have no idea of how I go about adaptin the php.net sample code. If someone could tell me I would be very greatful. I have the thing so it shows the files but it doesnt sort them. I think I have put each file into its own array, how do I prevent this.

Code: Select all

<?print('&lt;TABLE cellpadding=0 cellspacing=4 border=0 width=760>');
$dir = opendir("$id/");
$cols = 0;
while ($file = readdir($dir)) {  //first while
	$time=filemtime(&quote;$id/$file");  // gets filetime
	$times=array("$time"=>"$id/$file");  //puts into an array
	asort($times);  //sorts array
	reset($times); // resets array
	while (list($key, $val) = each($times)) { //lists the array
   if (!$cols){ print('&lt;tr&gt;');}  //from here it works fine
  if(is_file(&quote;$id/$file&quote;)){
  list($width, $height)=getimagesize(&quote;$id/$file&quote;);
  if($height&lt;$width){
  print(&quote;&lt;TD&gt;&lt;A href=\&quote;javascript:largeImage('$id/$file','',$width,$height)\&quote;&gt;&lt;img src=$id/$file alt=$file width=120 border=0&gt;&lt;/a&gt;&lt;/TD&gt;&quote;);
  }
  if($height&gt;=$width){
  print(&quote;&lt;TD&gt;&lt;A href=\&quote;javascript:largeImage('$id/$file'<TABLE cellpadding=0 cellspacing=4 border=0 width=760>');
$dir = opendir("$id/");
$cols = 0;
while ($file = readdir($dir)) {  //first while
	$time=filemtime("$id/$file");  // gets filetime
	$times=array("$time"=>"$id/$file");  //puts into an array
	asort($times);  //sorts array
	reset($times); // resets array
	while (list($key, $val) = each($times)) { //lists the array
   if (!$cols){ print('<tr>');}  //from here it works fine
  if(is_file("$id/$file")){
  list($width, $height)=getimagesize("$id/$file");
  if($height<$width){
  print("<TD><A href=\"javascript:largeImage('$id/$file','',$width,$height)\"><img src=$id/$file alt=$file width=120 border=0></a></TD>");
  }
  if($height>=$width){
  print("<TD><A href=\"javascript:largeImage('$id/$file','',$width,$height)\&quote;&gt;&lt;img src=$id/$file alt=$file width=80 border=0></a></TD>");
  }
  if ($cols > 2) {
    print('</tr>');
    $cols = 0;
  }else{
  	$cols++;
  }}
}}
print('</table>');
?>
AndyI have no idea of how I go about adaptin the php.net sample code. If someone could tell me I would be very greatful. I have the thing so it shows the files but it doesnt sort them. I think I have put each file into its own array, how do I prevent this.

Code: Select all

<?print('<TABLE cellpadding=0 cellspacing=4 border=0 width=760>');
$dir = opendir("$id/");
$cols = 0;
while ($file = readdir($dir)) {  //first while
	$time=filemtime("$id/$file");  // gets filetime
	$times=array("$time"=>"$id/$file");  //puts into an array
	asort($times);  //sorts array
	reset($times); // resets array
	while (list($key, $val) = each($times)) { //lists the array
   if (!$cols){ print('<tr>');}  //from here it works fine
  if(is_file("$id/$file")){
  list($width, $height)=getimagesize("$id/$file");
  if($height<$width){
  print("<TD><A href=\"javascript:largeImage('$id/$file','',$width,$height)\"><img src=$id/$file alt=$file width=120 border=0></a></TD>");
  }
  if($height>=$width){
  print("<TD><A href=\"javascript:largeImage('$id/$file','',$width,$height)\"><img src=$id/$file alt=$file width=80 border=0></a></TD>");
  }
  if ($cols > 2) {
    print('</tr>');
    $cols = 0;
  }else{
  	$cols++;
  }}
}}
print('</table>');
?>
Andy<?print('<TABLE cellpadding=0 cellspacing=4 border=0 width=760>');
$dir = opendir("$id/");
$cols = 0;
while ($file = readdir($dir)) { //first while
$time=filemtime("$id/$file"); // gets filetime
$times=array("$time"=>"$id/$file"); //puts into an array
asort($times); //sorts array
reset($times); // resets array
while (list($key, $val) = each($times)) { //lists the array
if (!$cols){ print('<tr>');} //from here it works fine
if(is_file("$id/$file")){
list($width, $height)=getimagesize("$id/$file");
if($height<$width){
print("<TD><A href=\"javascript:largeImage('$id/$file','',$width,$height)\"><img src=$id/$file alt=$file width=120 border=0></a></TD>");
}
if($height>=$width){
print("<TD><A href=\"javascript:largeImage('$id/$file','',$width,$height)\"><img src=$id/$file alt=$file width=80 border=0></a></TD>");
}
if ($cols > 2) {
print('</tr>');
$cols = 0;
}else{
$cols++;
}}
}}
print('</table>');
?>

Andy