Jump to content


Photo

Sort Directory


  • Please log in to reply
2 replies to this topic

#1 lunac

lunac
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts
  • LocationDulles, VA

Posted 20 September 2006 - 12:53 AM

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.  See example...

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.

$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>';
		
	}
}

LunaC
It's a crazy world....

#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 20 September 2006 - 03:33 AM

$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>';
}

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

#3 lunac

lunac
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts
  • LocationDulles, VA

Posted 21 September 2006 - 03:05 AM

That worked perfectly on the files! Thanks.

I tried that method on the folders, but I must be doing it wrong. And guidance there ?
LunaC
It's a crazy world....




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users