Jump to content

Help with forcing order of files by name (not using db)


frdk1

Recommended Posts

Title may not explain the problem clearly. I'll try to be clear.

First thing: I'm not very good at this PHP stuff.

The issue at hand: I have a PHP script that finds files (in this case images) within a defined folder and outputs a block with all files listed, and links set on each item. The script works exactly as anticipated on my local server using MAMP with PHP5 – files are listed in filename order (ex: img1, img2, img3) – but on my web server (also using PHP5) they are printed in a completely jumbled (dis)order. Ex: img2, img1, img3. My goal then is to force the order of appearance so that I can feel confident files are always printed in the expected, natural order.

 

This script, which is a standalone affair, does not use any db connection to do its thing. The following code shows the main processing bits. Question is -straightforwardly- how can I force the order? I've been looking at "order by ..." but being the PHP dunce I am, the answer eludes me.

Any help is much appreciated. Thanks.

 

<?php 
// Set to the directory that contains galleries
$dirPath = '' . (($galleryDir !== '') ? ($galleryDir . '/') : '') ;

if ($useTitle && ($galleryTitle !== '')) {
echo "<h3>$galleryTitle</h3>";
}

// Process main
if ($useThumbsDir) {
$dirPathThumbs = $dirPath . 'thumbs/';
show_gallery($dirPathThumbs, $thumbImgClass);
}
else {
show_gallery( $dirPath, $mainImgClass );
}

function get_gallery($dirPath, $imgClass = '') {
$dirObj = dir( $dirPath );
$i = 0;
$strOutput = "";
while (false !== ($entryName = $dirObj->read())) {
	if (is_file( $dirPath . $entryName )) {
		$fileInfo = pathinfo( $dirPath . $entryName );
		$fileExtension = $fileInfo['extension'];
		$extLength = strlen($fileExtension);
		$entry = substr($entryName, 0, -$extLength-1);
		$viewLink = '<a href="path-to-folder/'.$entryName.'" title="'.$entry.'">';

		if ( is_image_extension( $fileInfo["extension"] ) ) {
		++$i;
		$strOutput .= '
		<div class="thumb left">'.$viewLink.'<img class="'.$imgClass.'" src="'.$dirPath.$entryName.'" alt="'.$entry.'" /></a></div>';
		}
	}
} // End while
if ( $i > 0 ) {
	$strOutput = $strOutput.'<div class="clearer"></div>';
}
$dirObj->close();
return $strOutput;
}

function show_gallery( $dirPath, $imgClass = '') {
   if ( $strGallery = get_gallery($dirPath, $imgClass) )
      echo $strGallery;
}

function is_image_extension( $strExtension = '') {
   if ( ($strExtension > '') && in_array( $strExtension, array( 'gif', 'png', 'jpg', 'jpe', 'jpeg' ) ))
      return true;
   return false;
}
?>

 

I should perhaps mention that the script is included within a sNews environment (sNews is PHP/MySQL based) but it works independently so unless I'm missing something that shouldn't really affect things.

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.