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
https://forums.phpfreaks.com/topic/21340-sort-directory/
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
https://forums.phpfreaks.com/topic/21340-sort-directory/#findComment-95063
Share on other sites

Archived

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

×
×
  • 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.