|
-
Jun 9th, 2002, 05:14 PM
#1
Thread Starter
Stuck in the 80s
Dirs and Files
I'm running this code:
PHP Code:
if ($handle = opendir('/home/vbshelf/public_html')) {
$d = 0; $f = 0;
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($file)) {
$dirs[$d] = $file;
$d = $d + 1;
} else {
$files[$f] = $file;
$f = $f + 1;
}
}
}
closedir($handle);
sort($dirs);
sort($files);
for ($i = 0; $i < count($dirs); $i++) {
echo "<b>" . $dirs[$i] . "</b><br>\n";
}
for ($i = 0; $i < count($files); $i++) {
echo $files[$i] . "<br>\n";
}
}
This is the only thing I could come up with to sort the directories and files in alphabetical order. Is this the best way to do it, or is there an easier approach?
-
Jun 9th, 2002, 05:42 PM
#2
Member
Here's my script that I use for my avatars on TT...it just happens to show stuff in alpha order for some reason. I was teh major PHP n00b at the time so ignore the stupidities:
PHP Code:
<?
$basename = basename($PATH_TRANSLATED);
$windowtitle = ereg_replace("$basename", "", $PATH_TRANSLATED);
$current_dir = $windowtitle;
include('/home/dh2255/header.php');
if ($current_dir != "/")
{
echo "<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\"><a href=\"../\">
Go up a directory</a></font><br><br>";
}
?>
<table border="0" cellspacing="0" cellpadding="6">
<tr bgcolor="#000099">
<td><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#FFFFFF">File
name</font></b></td>
<td><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#FFFFFF">Size
(bytes)</font></b></td>
<td><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#FFFFFF">Last
Modified</font></b></td>
</tr>
<?
$bitflip = false;
$handle = opendir('.');
while (false !== ($file = readdir($handle)))
{
$bitflip = !$bitflip;
if ($bitflip)
{
echo "<tr>";
}
else
{
echo "<tr bgcolor=\"#EEEEEE\">";
}
if ($file != "." && $file != "..")
{
if (is_dir($file))
{
?><td colspan="3"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><?
echo "Directory: "<a href=\"$file/\">$file</a>";
?></font></td><?
}
else
{
?><td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><?
echo "<a href=\"$file\">$file</a>";
if (eregi("gif$|png$|jpg$|bmp$|jpg$|jpeg$", $file))
{
echo ":<br><img src=\"$file\">";
}
?></font></td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="2">
<?
echo filesize($file);
?>
</font></td>
<td><font face="Verdana, Arial, Helvetica, sans-serif" size="2">
<?
$filemod = filemtime($file);
echo date("F j, Y h:i:s A", $filemod);
?></font></td><?
}
}
?></tr><?
} ?>
</table>
<?
include('/home/dh2255/footer.php');
?>
-
Jun 9th, 2002, 05:57 PM
#3
Thread Starter
Stuck in the 80s
That doesn't do it in order for me for some reason?
-
Jun 9th, 2002, 06:44 PM
#4
Fanatic Member
Re: Dirs and Files
His will not sort it. There is no algorithm for sorting in his.
Originally posted by The Hobo
I'm running this code:
PHP Code:
if ($handle = opendir('/home/vbshelf/public_html')) {
$d = 0; $f = 0;
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($file)) {
$dirs[$d] = $file;
$d = $d + 1;
} else {
$files[$f] = $file;
$f = $f + 1;
}
}
}
closedir($handle);
sort($dirs);
sort($files);
for ($i = 0; $i < count($dirs); $i++) {
echo "<b>" . $dirs[$i] . "</b><br>\n";
}
for ($i = 0; $i < count($files); $i++) {
echo $files[$i] . "<br>\n";
}
}
This is the only thing I could come up with to sort the directories and files in alphabetical order. Is this the best way to do it, or is there an easier approach?
The only thing you can really change is taking out the $d and $f and the incrementing of those values in the while loop. All you need is [] as that will add the value to the next array.
-Matt
-
Jun 9th, 2002, 07:07 PM
#5
PowerPoster
Originally posted by filburt1
I was teh major PHP n00b at the time
you still are aren't you?
-
Jun 9th, 2002, 07:08 PM
#6
Thread Starter
Stuck in the 80s
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|