need help sorting list of files

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
cruddell001
Forum Newbie
Posts: 3
Joined: Wed Jan 05, 2011 9:58 am

need help sorting list of files

Post by cruddell001 »

Hi,
I'm new to PHP, but have used Javascript quite a bit before. I wanted to create a script that retrieves filenames from a folder on my webserver, then reverse sorts those files and makes them into clickable links. After some searching on Google, I decided PHP was the way to go, and so I wrote the following script. The filenames are in the format "Entos_Web_MM-DD-YY.pdf", and I use the substr command to shorten the text of the links to just "MM-DD-YY". The problem I'm having is that this only sorts based on month first, then day, then year (as would be expected since that is the order the characters are in). However, I'd like it to sort it on year first, so that the ones from January 2011 come before December 2010 files. I'm sure there's a way to do this by using the substr command to get only the "YY" portion of the filename and sort based on that, but I'm struggling with how exactly to do that and put it back into the array.

Here is the script:

Code: Select all

<?php
 if ($handle = opendir('newsletter')) {
   $filenames = array();
   $filenames2 = array();
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != "..")
	  {
	  $filenames[] = $file;

          }
     rsort($filenames);


       }
  closedir($handle);
  }

$i = 0;
while ($i <= count($filenames)) {
	$thelist .= '<a target="_blank" href="newsletter/'.$filenames[$i].'">'.substr($filenames[$i],10,-4).'</a><br>';
	$i++;
}

 	 $thelist2 = implode($filenames);

?>

<head>
<style type="text/css">
body
{
background-color: #999966;
overflow: auto;
}

A:link {text-decoration: none; color: blue; outline:none}
A:visited {text-decoration: none; color: blue; outline:none}
A:active {text-decoration: none; color: blue; outline:none}
A:hover {color: red; text-decoration: none; outline:none}
</style>


</head>

<body>

<img src="images/entos.jpg" style="width:100px;">
<br>
Newsletter Archive
<br>
<br>

<P><?=$thelist?></p>

</body>
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: need help sorting list of files

Post by VladSun »

take a look at the glob() function in PHP
There are 10 types of people in this world, those who understand binary and those who don't
cruddell001
Forum Newbie
Posts: 3
Joined: Wed Jan 05, 2011 9:58 am

Re: need help sorting list of files

Post by cruddell001 »

VladSun wrote:take a look at the glob() function in PHP
I'm not sure I understand how that will help. I can see how it might give me an array of all files that have a specified value, such as "11" for the year, but that doesn't seem very clean since it requires the hard-coding of the year. Am I missing something? Perhaps an example would help.
cruddell001
Forum Newbie
Posts: 3
Joined: Wed Jan 05, 2011 9:58 am

Re: need help sorting list of files

Post by cruddell001 »

okay - after some trial and error, I figured it out:

Code: Select all

<?php
 if ($handle = opendir('newsletter')) {
   $filenames = array();
      $filenames1 = array();
   $filenames2 = array();
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != "..")
	  {
	  $filenames[] = $file;
           }
        }


  closedir($handle);
  }


    sort($filenames);

   $var1 = substr($filenames[0], -6, 2);
     rsort($filenames);

   $var2 = substr($filenames[0], -6, 2);
    $globtxt = "newsletter/*".$var1.".pdf";
   $globtxt2 = "newsletter/*".$var2.".pdf";
   $filenames1[] = glob($globtxt);
   $filenames2[] = glob($globtxt2);



if ($var1 != $var2){

   rsort($filenames1[0]);
   rsort($filenames2[0]);


   $i = 0;
while ($i < count($filenames1[0])) {
	$thelist .= '<a target="_blank" href="'.$filenames1[0][$i].'">'.substr($filenames1[0][$i],21,-4).'</a><br>';
	$i++;
}


   $i = 0;
while ($i < count($filenames2[0])) {
	$thelist .= '<a target="_blank" href="'.$filenames2[0][$i].'">'.substr($filenames2[0][$i],21,-4).'</a><br>';
	$i++;
}

}

if ($var1 == $var2){
$i = 0;
while ($i <= count($filenames)) {
	$thelist .= '<a target="_blank" href="newsletter/'.$filenames[$i].'">'.substr($filenames[$i],10,-4).'</a><br>';
	$i++;
}
}


?>

<head>
<style type="text/css">
body
{
background-color: #999966;
overflow: auto;
}

A:link {text-decoration: none; color: blue; outline:none}
A:visited {text-decoration: none; color: blue; outline:none}
A:active {text-decoration: none; color: blue; outline:none}
A:hover {color: red; text-decoration: none; outline:none}
</style>


</head>

<body>

<img src="images/entos.jpg" style="width:100px;">
<br>
Newsletter Archive
<br>
<br>

<P><?=$thelist?></p>

</body>
Post Reply