Jump to content

Archived

This topic is now archived and is closed to further replies.

lunac

Sort Directory

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]

Share this post


Link to post
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..

Share this post


Link to post
Share on other sites
That worked perfectly on the files! Thanks.

I tried that method on the folders, but I must be doing it wrong. And guidance there ?

Share this post


Link to post
Share on other sites

×

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.