Jump to content

sorting


srider

Recommended Posts

Hi,

 

I really don't know much about php but need a quick fix.  The code below creates a list of images in a directory and outputs an XML file.  The only issue I have is that they are not sorted.  Can anyone tell me how to change this file so that it outputs the image names to the XML file alphabetically?

<?php
require_once('JPEG-Meta.php');
header('Content-type: text/xml');

function create_tree($file_dir, $web_dir){
$masterXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$fileXML = "";
	if ($handle = @opendir($file_dir)){
	$i=0;
		while (false !== ($file = @readdir($handle))){
			if ($file != "." && $file != ".."){
			 $list[$i]=$file;
			 $i++;
			}
	}
		$dir_length = count($list);
	$dirXML = "<gallery>\n";
	for($i=0; $i<$dir_length; $i++){
		if(strrpos($list[$i], ".") != FALSE && strrpos($list[$i], ".jpg") != FALSE){
				$jpegMeta = new JPEG($file_dir.$list[$i]);
				$caption = $jpegMeta->getIPTCField("Caption");
				$metaWidth = $jpegMeta->getWidth();
				$metaHeight = $jpegMeta->getHeight();
				$info = getimagesize($file_dir.$list[$i]);
				$imgW = $info[0] > 0 ? $info[0] : $metaWidth;
				$imgH =  $info[1]> 0 ? $info[1] : $metaHeight; 
				$fileXML .= "\t<img width=\"".$imgW."\" height=\"".$imgH."\" 

src=\"".$web_dir.$list[$i]."\" caption=\"".$caption."\" filename=\"".$list[$i]."\" />\n";
			} 
		}
	$masterXML .= $dirXML.$fileXML."</gallery>";
	closedir($handle);
	} 
return $masterXML;
}

$web_dir = $_GET['dir'];

if(substr($web_dir, 0, 1) == "/"){
$doc_root = str_replace($_SERVER['PHP_SELF'], '', str_replace('\\', '/',$_SERVER["SCRIPT_FILENAME"]));
} else {
$doc_root  = str_replace(basename($_SERVER['PHP_SELF']), '', str_replace('\\', '/',$_SERVER["SCRIPT_FILENAME"]));
} 

if(substr($web_dir, -1) != "/"){
$web_dir .= "/";
}

$dir = $doc_root.$web_dir;
echo utf8_encode(create_tree($dir, $web_dir));
?>

 

Thanks a lot!

Link to comment
https://forums.phpfreaks.com/topic/192000-sorting/
Share on other sites

Thank you so much for the reply.  It still doesn't work.  I apologize if I'm missing something easy but I really know nothing about php.  I added that line:

 

$dir = $doc_root.$web_dir;

sort($list);

echo utf8_encode(create_tree($dir, $web_dir));

?>

 

Yet it still does not work.  The files are still being added into flash randomly.  Any more thoughts on it?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/192000-sorting/#findComment-1012027
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.