Jump to content

Help with alphabetizing folder contents


jay1318

Recommended Posts

First of all - wonderful site! Great people and very helpful for a newbie like me.

 

I have a simple php script that shows the files in a particular folder. Its working great but I need the results to return alphabetized. Much thanks in advance!

 

Here's my code...

 

<?PHP

 

$folder =

 

"/my/complete/path/";

 

 

 

$handle = opendir($folder);

 

# Making an array containing the files in the current directory:

while ($file = readdir($handle))

 

 

{

 

  $files[] = $file;

  }

 

  closedir($handle);

 

  #echo the files

  foreach ($files as $file) {

 

 

if($file == "." || $file == ".." || $file == "index.php" || $file == "thisfilename.php")

 

  continue;

  echo "<li><a href=$file>$file</a></li>"."<br />";

  }

 

  ?>

 

 

Link to comment
Share on other sites

That's it? Cool. But, can you possibly tell me where exactly to place it in the code or what it might replace?

 

Thanks again!

 

Considering that sort takes an array, you may want to give it the array with the folder names.

Link to comment
Share on other sites

Well, still having trouble. This script is actually one I found online somewhere. Only modified it to not show the name of the php file itself.

 

Not sure why its returning the files out of order. Figured they'd be alphabetized by default.

Link to comment
Share on other sites

If you don't use kenrdnsn's solution, then you're going to have to invoke the sort method on the array that contains all the file names ($files).  glob is made for such a task, you should probably use it instead.

Link to comment
Share on other sites

In reading about the glob function, it does indeed appear to be the way to go. Unfortunately, as such a php novice, having trouble incorporating it correctly into the current script.

 

What's the problem you're having?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.