/    Sign up×
Community /Pin to ProfileBookmark

Sort Directory Listing

Hi all,

I’m attemping to write my first PHP script to format and list the contents of a directory. Here’s what I have so far…

[CODE]

// …all HTML/CSS before this point…

<?php

$dir=”.”; // Directory where files are stored

if ( array($dir_list = opendir($dir)) && ($dir_list2 = opendir($dir)) ) {

$count = 0;

while ( (($filename = readdir($dir_list)) !== false) && (($prettyfilename = readdir($dir_list2)) !== false) ) {
$count = $count + 1;

switch ($prettyfilename) {
case (stristr($prettyfilename,”AD”)):
$prettyfilename = str_ireplace(“AD” , “Adelaide” , $prettyfilename);
break;
case (stristr($prettyfilename,”AK”)):
$prettyfilename = str_ireplace(“AK” , “Auckland” , $prettyfilename);
break;
case (stristr($prettyfilename,”BN”)):
$prettyfilename = str_ireplace(“BN” , “Brisbane” , $prettyfilename);
break;
case (stristr($prettyfilename,”BK”)):
$prettyfilename = str_ireplace(“BK” , “Burke Road” , $prettyfilename);
break;
case (stristr($prettyfilename,”CA”)):
$prettyfilename = str_ireplace(“CA” , “Canberra” , $prettyfilename);
break;
}

if( (is_int($count/2)) === TRUE){
echo “<tr>”;
echo “<td width=”200″ class=”oddrow”><a href=””.$filename.””>”.$prettyfilename.”</a></td>”;
echo “<td width=”200″ class=”oddrow”><i>[Earliest Archive]</i></td>”;
echo “<td width=”200″ class=”oddrow”><i>[Latest Archive]”.$count.”</i></td>”;
echo “</tr>”;
}else{
echo “<tr>”;
echo “<td width=”200″ class=”evenrow”><a href=””.$filename.””>”.$prettyfilename.”</a></td>”;
echo “<td width=”200″ class=”evenrow”><i>[Earliest Archive]</i></td>”;
echo “<td width=”200″ class=”evenrow”><i>[Latest Archive]”.$count.”</i></td>”;
echo “</tr>”;
}

}

closedir($dir_list);
closedir($dir_list2);
}
?>

// …all HTML/CSS after this point…

[/CODE]

What I can’t figure out is how to sort this listing alphabetically. I have tried working with arrays but can never get it to work.

Can someone show me how to do this using the code above?

Many thanks!

Chris

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@SyCoDec 15.2008 — Right now you're reading the directory and echoing in the same loop. If you build an array in the loop then close it you can then sort the array and loop the new array to display in the new order.
Copy linkTweet thisAlerts:
@Chris_JacksauthorDec 16.2008 — Hi SyCo,

Thanks for the reply! Here's my attempt at what you suggested:

[CODE]$dir="."; // Directory where files are stored

if ( array($dir_list = opendir($dir)) && ($dir_list2 = opendir($dir)) ) {

$count = 0;


while ( (($filename = readdir($dir_list)) !== false) && (($prettyfilename = readdir($dir_list2)) !== false) ) {
switch ($prettyfilename) {
case (stristr($prettyfilename,"AD")):
$prettyfilename = str_ireplace("AD" , "Adelaide" , $prettyfilename);
break;
case (stristr($prettyfilename,"AK")):
$prettyfilename = str_ireplace("AK" , "Auckland" , $prettyfilename);
break;
case (stristr($prettyfilename,"BN")):
$prettyfilename = str_ireplace("BN" , "Brisbane" , $prettyfilename);
break;
case (stristr($prettyfilename,"BK")):
$prettyfilename = str_ireplace("BK" , "Burke Road" , $prettyfilename);
break;
case (stristr($prettyfilename,"CA")):
$prettyfilename = str_ireplace("CA" , "Canberra" , $prettyfilename);
break;
}
$filenamearray = array($filename);
$prettyfilenamearray = array($prettyfilename);
}


while ( ($filenamearray !== false) && ($prettyfilenamearray !== false) ) {

$count = $count + 1;

if( (is_int($count/2)) === TRUE){
echo "<tr>";
echo "<td width="200" class="oddrow"><a href="".$filenamearray."">".$prettyfilenamearray."</a></td>";
echo "<td width="200" class="oddrow"><i>[Earliest Archive]</i></td>";
echo "<td width="200" class="oddrow"><i>[Latest Archive]".$count."</i></td>";
echo "</tr>";
}else{
echo "<tr>";
echo "<td width="200" class="evenrow"><a href="".$filenamearray."">".$prettyfilenamearray."</a></td>";
echo "<td width="200" class="evenrow"><i>[Earliest Archive]</i></td>";
echo "<td width="200" class="evenrow"><i>[Latest Archive]".$count."</i></td>";
echo "</tr>";
}

}

closedir($dir_list);
closedir($dir_list2);
}[/CODE]


This seems to just hang (looping forever?) until I hit stop on the browser. I can't figure out how to rememdy this. Any suggestions?

Thanks again!

Chris
Copy linkTweet thisAlerts:
@SyCoDec 16.2008 — only read the directory once. Build your array in the first loop then foreach() the array for the second loop.
Copy linkTweet thisAlerts:
@Chris_JacksauthorDec 16.2008 — Am I building the array correctly in the first loop (is that the correct syntax)?
Copy linkTweet thisAlerts:
@SyCoDec 16.2008 — Sorry no time right now, read the manual entry for readdir() and all the examples in the user comments. You should be able to google it too.
×

Success!

Help @Chris_Jacks spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...