Jump to content

@#^$&% thing won't alphabetize...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
bostonjim

bostonjim

    Newbie

  • Members
  • Pip
  • 7 posts
Here is the code I'm using. It is supposed to display the contents of a folder in alphabetical order. But it's not alphabetizing.

<?php

$THUMBNAIL_FOLDER = "_thumbnails";

$HIDDEN_FILES[]	= "Thumbs.db";

$HIDDEN_FILES[] = "index.php";

$HIDDEN_FILES[] = $THUMBNAIL_FOLDER;

$THUMBNAIL_WIDTH = 200;

$THUMBNAIL_HEIGHT = 100;


// Image Output;

HandleImageOutput();


?>

<html>

<head>

<title>Index Of.</title>

<style>

body { background-color: #FCFFF0; font-size: 12px;}

A:link {text-decoration: none; color: blue}

A:visited {text-decoration: none; color: blue}

A:active {text-decoration: none; color: blue}

A:hover {text-decoration: underline; color: blue}


#images, #folders, #files { border: 1px solid #000000; background-color: #fff; margin: 50px; padding: 25px; }



DIV.image { width: <?php echo $THUMBNAIL_WIDTH; ?>; height: <?php echo $THUMBNAIL_HEIGHT; ?>; float: left; margin: 10px; text-align: center; border: 0px; }

IMG { border: 1px solid #444; background-color: #eee; }

</style>

</head>

<body>


<p><br><p><table width=100%><tr width=100%><td width=100%><h2>Title</h2>

<font face=tahoma>


<?php


$Browse = $_GET["b"] ;


if ( $Browse )

{

	$Browse .= "/";

}


// Stop them accessing higher level folders

if ( substr_count( $Browse, ".." ) > 0 || substr_count( $Browse, $THUMBNAIL_FOLDER ) > 0  )

{

	echo "You don't have permission to access this folder!";

	exit();

}


$DIR = "./" . $Browse;

$d = dir( $DIR );


while (false !== ($entry = $d->read())) 

{

	if ($entry[0] == '.') continue;

	if ( in_array( $entry, $HIDDEN_FILES) ) continue;


	// Don't list the folder if it has an index!

	if ($entry == 'index.html') exit();

	if ($entry == 'index.htm') exit();

	if ($entry == 'index.php') exit();

	if ($entry == 'index.asp') exit();

	

	if ( IsImage($entry) ) $images[] = array( filemtime( $DIR . "/" . $entry ), $entry );

	else if ( is_dir( $DIR . "/" . $entry ) ) $folders[] = array( filemtime( $DIR . "/" . $entry ), $entry );

	else $files[] = array( filemtime( $DIR . "/" . $entry ), $entry );


}



if ( count( $folders ) > 0 )

{

	echo "<div id=\"folders\">";


	arsort($folders);

	foreach ( $folders as $fn )

	{

		echo "<a href=\"$fn[1]/index.php\">$fn[1]</a><br>";

	}

	

	echo "</div>";

}


if ( count( $files ) > 0 )

{

	echo "<div id=\"files\">";


	arsort($files);

	foreach ( $files as $fn )

	{

		echo "<a href=\"$Browse$fn[1]\">$fn[1]</a><br>";

	}

	

	echo "</div>";

}


if ( count( $images ) > 0 )

{

	echo "<div id=\"images\"><div style=\"clear: both;\"></div>";


	arsort($images);

	foreach ( $images as $fn )

	{

		echo GetImageLink( $Browse . $fn[1], $fn[1] );

	}

	

	echo "<div style=\"clear: both;\"></div></div>";

}


?>



</body>

</html>


<?


 ////// FUNCTIONS /////

 

	function IsImage( $i )

	{

		$i = strtolower( $i );

		

		if ( substr_count( $i, ".jpg" ) > 0  ||

			 substr_count( $i, ".jpeg" ) > 0 ||

			 substr_count( $i, ".gif" ) > 0 ||

			 substr_count( $i, ".png" ) > 0 )

		{

			return true;

		}

		

		return false;

	}

	

	

	function GetImageLink( $imgfilename, $img )

	{

		global $THUMBNAIL_WIDTH, $THUMBNAIL_HEIGHT;

		return "<div class=\"image\"><a href=\"$imgfilename\"><img src=\"?img=$imgfilename\" width=$THUMBNAIL_WIDTH height=$THUMBNAIL_HEIGHT></a><br>$img</div>";

	}

	

	function HandleImageOutput()

	{

		global $THUMBNAIL_FOLDER, $THUMBNAIL_WIDTH, $THUMBNAIL_HEIGHT;

		

		$image = $_GET['img'];

		if (!$image) return;

		

		$imagecache = $THUMBNAIL_FOLDER . "/" . md5( $THUMBNAIL_WIDTH . $THUMBNAIL_HEIGHT . $image ) . ".jpg";

		

		$im = @imagecreatefromjpeg( $imagecache );

		if ( $im )

		{

			header("Content-type: image/jpg");

			imagejpeg($im);							

			imagedestroy($im);					

			exit();

		}

		

		$imgsize = getimagesize ( $image );

		

		switch ($imgsize[2]) 

		{

	  		case 1: // GIF

	    		$im    	= imagecreatefromgif( $image );

	    		break;

	   		case 2: // JPG

	    		$im    	= imagecreatefromjpeg( $image );

	    		break;

	   		case 3: // PNG

	    		$im    	= imagecreatefrompng( $image );

	    		break;

	    	default: // UNKNOWM!

				echo "Unknown Image!";

	    		exit();		

		}

		

		header("Content-type: image/jpg");


		$img_thumb = imagecreatetruecolor( $THUMBNAIL_WIDTH, $THUMBNAIL_HEIGHT );

		

		$dsth = ($THUMBNAIL_WIDTH / ImageSX($im)) * ImageSY($im);

		

		imagecopyresampled( $img_thumb, $im, 0,($THUMBNAIL_HEIGHT-$dsth)/2, 0,0, $THUMBNAIL_WIDTH, $dsth, ImageSX($im), ImageSY($im) );

		

		imagejpeg( $img_thumb );

		

		// This will fail if you haven't created and chmodded your thumbnails folder

		if ( $im && $img_thumb )

		{

			@imagejpeg( $img_thumb, $imagecache, 95 );

		}

		

		imagedestroy( $img_thumb );

		imagedestroy( $im );

		exit();

	}

?>


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Try using natsort():
PHP: natsort - Manual

From what I see there's nothing wrong.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
bostonjim

bostonjim

    Newbie

  • Members
  • Pip
  • 7 posts
Unfortunately that didn't work. I also just noticed something funky. Using the sorting I have in my code, it actually is sorting... but the sorting is broken up into two groups. First it runs through the shorter names alphabetically, then it starts over at "A" and runs through the longer names alphabetically. In other words, wtf?

#4
bostonjim

bostonjim

    Newbie

  • Members
  • Pip
  • 7 posts
And now, to test whether it is somehow related to the length of the entries being sorted, I shortened the name of one of them. But it still appears in the same position. So I guess it's just a coincidence that the longer ones are grouped together and there is something else affecting the sort.

I really don't get this. I just want the array to sort in alphabetical order. But for some reason it seem to be separating some from the others and then, secondarily, sorting the two groups alphabetically...

#5
bostonjim

bostonjim

    Newbie

  • Members
  • Pip
  • 7 posts
Accidental double-post

#6
bostonjim

bostonjim

    Newbie

  • Members
  • Pip
  • 7 posts
Well, I've pretty much narrowed down the problem. When I make some change to a file in the folder, that folder pops to the top of the list in its parent folder (where I have this php code displaying the contents of that folder). So it is actually sorting them by the date of the folder's contents. The partially alphabetical sorting is just due to the fact that they mostly uploaded in that order.

ANY help on why it would be doing this would be appreciated...

#7
bostonjim

bostonjim

    Newbie

  • Members
  • Pip
  • 7 posts
Well, I figured it out myself. The use of filemtime instead of basename in the array was the problem.