Jump to content

Sort Directory


lunac

Recommended Posts

I wrote a quick and dirtly little script that allows me to drop some comp images into folders for my clients for them to view. Basically it looks at a directory/file and changes it's name from 1MyLittlePumpkin to My Little Pumkin. These show in a list with thumbnails below the directory name.  [url=http://www.bythemoon.com/clientfiles/tucker/comp/]See example...[/url]

I hoped to be able to sort directories and files by the number I placed in front, but I can't figure it out. Any guidance would be appreciated.

[code=php:0]
$fdir = opendir(".");
while($folder = readdir($fdir)){
$start = substr($folder, 0, 1);
if($start != "." and $folder != "index.php" and $folder != "image.php"){
$ftitle = substr($folder, 1);
$ftitle = preg_replace("/([A-Z0-9])/", " $1", $ftitle);
echo "<h4>$ftitle </h4>";
$dir = opendir($folder);
while($file = readdir($dir)){
$start = substr($file, 0, 1);
if($start != "."){
$title = substr($file, 1);
$title = preg_replace("/\..*?$/", "", $title);  // removes the extension
$title = preg_replace("/([A-Z0-9])/", " $1", $title);  // places the space
echo '<div class="thumb"><a href="image.php?folder=' . $folder . '&img=' . $file . '" target="view"><img src="' . $folder . "/" . $file . '"></a><br />' . $title . '</div>';

}
}
[/code]
Link to comment
Share on other sites

[code]$files = array();
while ($file = readdir($dir)) {
  $start = substr($file, 0, 1);
  if ($start != ".") {
    $files[] = $file;
  }
}
sort($files)
foreach ($files as $file) {
  $title = substr($file, 1);
  $title = preg_replace("/\..*?$/", "", $title);  // removes the extension
  $title = preg_replace("/([A-Z0-9])/", " $1", $title);  // places the space
  echo '<div class="thumb"><a href="image.php?folder=' . $folder . '&img=' . $file . '" target="view"><img src="' . $folder . "/" . $file . '"></a><br />' . $title . '</div>';
}[/code]

That ought to work.  Instead of reading the files and displaying them immediately, you will be reading them into an array, sorting the array, then displaying them.  I've left the filtering of "." files in the first foreach, so you don't need to do it again in the second one..
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.