Re: help sorting readdir values

From: Date: Tue, 29 Jan 2002 03:16:26 +0000
Subject: Re: help sorting readdir values
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-82507@lists.php.net to get a copy of this message
In article <20020128164934.GC31885@lunar.radom.org>, dan@radom.org says... > First I must state that I didn't write the code, and I'm not much of a programmer of > any kind. I'm hoping someone can tell me how to sort the filenames and file sizes returned by > readdir and filesize in the below code. Thanks in advance. > > dan > > > <? > > function list_dir($dir) > { > $handle=opendir($dir); > echo "<ul>"; > while ($file = readdir($handle)) > { > if ($file != '.' && $file != '..' && > !ereg("\.php$", $file)) { > print ("<tr>\n<td>\n"); > print ("<li><a > > href=\"".$GLOBALS["path"].$file."\">".$file."</a>"); > print ("</td>\n"); > if (is_dir($file)) > { > printf ("<td>"); > $toto = $GLOBALS["path"]; > $GLOBALS["path"] .= > $file."/"; > chdir ($file); > list_dir("."); > $GLOBALS["path"] = $toto; > chdir (".."); > printf ("</td>"); > } else { > printf ("<td>\n"); > $size = filesize($file); > if ($size == NULL) > $size = "<unknown>"; > printf ("&nbsp;".$size." > &nbsp;bytes\n"); > printf ("</td>\n"); > } > print ("</tr>\n"); > } > } > echo "</ul>"; > closedir($handle); > } > > $directory = "screenshots"; > > $GLOBALS["path"] = > "http://".$HTTP_HOST."/".$directory."/"; > > echo "<table>\n"; > list_dir("."); > echo "</table>"; The short answer (well, one of) is that you extract your file information into arrays, which you can then sort using the various array sorting tools. Tthen, having sorted the aray as you desire, step through the array and display the contents with whatever formatting you need. If you need more help with this, I think I have some functions which could be fairly easily modified to use file size as well as name; but I'll have to scratch around in the Linux partition of my lapdog to find and test them. -- David Robley Temporary Kiwi!

« previous php.general (#82507) next »