Sort by read dir

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simkrist
Forum Newbie
Posts: 2
Joined: Mon Feb 08, 2010 3:08 pm

Sort by read dir

Post by simkrist »

Hi

I'm trying to make a xml file using read dir and mark it with a number. So far, so good.. but How do I sort it by name, its seems to sort it by created date.

Can someone please help me

Regards
Stoijan

Code: Select all

<?php header('Content-Type: text/xml');
 
ini_set('allow_url_fopen', 'On');
ini_set('allow_url_include', 'On');
 
$path = "/";
$dirname=$_GET["id"];
$counter=0;
$fildir = "$dirname/";
$thumbdir = "$dirname/tumbs/";
//$dirname = ".";
 
if ($dirname == ""){
   echo "<?xml version='1.0' encoding='iso-8859-1' standalone='yes'?>";
   echo "<order baseuri='".$path."/' imagecount='".$counter."' orderno='".$dirname."' videovisning='http://www.nn.nn/streaming/".$dirname."/videovisning.html'>";
   echo "<status>";
   echo "<code>Failed</code>";
   echo "<active>false</active>";
   echo "<refnr>".$dirname."</refnr>";
   echo "<message>Du tastet ikke inn et referansenummer</message>";
   echo "</status>";
   echo "</order>";
} elseif (! is_dir($dirname)) {
   echo "<?xml version='1.0' encoding='iso-8859-1' standalone='yes'?>";
   echo "<order baseuri='".$path."/' imagecount='".$counter."' orderno='".$dirname."' videovisning='http://www.nn.nn/streaming/".$dirname."/videovisning.html'>";
   echo "<status>";
   echo "<code>Failed</code>";
   echo "<active>false</active>";
   echo "<refnr>".$dirname."</refnr>";
   echo "<message>Referansenummeret du tastet inn er ukjent</message>";
   echo "</status>";
   echo "</order>";
}
else {
  $dh = opendir($dirname) or die("Ukjent id: ".$dirname);
 
  while (!(($file = readdir($dh)) === false ) ) {
   if (! is_dir("$dirname/$file")) {
   $counter=$counter+1;
   }
  }
 
 
  echo "<?xml version='1.0' encoding='iso-8859-1' standalone='yes'?>";
  echo "<order baseuri='".$path."/' imagecount='".$counter."' orderno='".$dirname."' videovisning='http://www.nn.nn/streaming/".$dirname."/videovisning.html'>";
  echo "<status>";
  echo "<code>OK</code>";
  echo "<active>true</active>";
  echo "<refnr>".$dirname."</refnr>";
  echo "<message />";
  echo "</status>";
  echo "<images>";
 
  $counter=0;
 
  closedir($dh);
 
  $dh = opendir($dirname) or die("Ukjent id: ".$dirname);
 
  while (!(($file = readdir($dh)) === false ) ) {
   if (! is_dir("$dirname/$file")) {
   $sort=$counter+1;
   $thumbsize = getimagesize($thumbdir.$file);
   $storsize = getimagesize($fildir.$file);
   $thumbfilesize = filesize($thumbdir.$file);
   $originalfilesize = filesize($fildir.$file);
 
   $string = "$file";
   $bildenavn = basename($string,".jpg");
   echo "<image id='10".$counter."' sortorder='".$sort."' title='".$bildenavn."'>";
   echo "<format dpi='150' mime='$thumbsize[mime]' filesizeKB='$thumbfilesize' $thumbsize[3] name='thumbnail' relurl='".$dirname."/tumbs/".$file."'/>";
   echo "<format dpi='150' mime='$storsize[mime]' filesizeKB='$originalfilesize' $storsize[3] name='original' relurl='".$dirname."/".$file."'/>";
   echo "</image>";
   $counter=$counter+1;
   }
  }
  echo "</images>";
  echo "</order>";
  closedir($dh);
}
 
?>
 
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Sort by read dir

Post by Christopher »

You can sort the array, or take a look at glob().
(#10850)
simkrist
Forum Newbie
Posts: 2
Joined: Mon Feb 08, 2010 3:08 pm

Re: Sort by read dir

Post by simkrist »

Arghhh... can't make it work. Still gets it sorted by date.
Post Reply