How do I sort files in date order??

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
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

How do I sort files in date order??

Post 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
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

When a file is uploaded though, it's also modified. fileatime() would also work would it not?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post 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.
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post 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
-
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post 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
Post Reply