homepage Welcome to WebmasterWorld Guest from 54.237.78.165
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Help listing directory files sorting
chrissim

5+ Year Member



 
Msg#: 4412993 posted 8:40 am on Feb 1, 2012 (gmt 0)

hi,

Can someone show me and guide me how to sort directory files in alpabetical order below?



<?
if (is_dir($_SETTINGS['DIR'])) {
$dir = opendir("{$_SETTINGS['DIR']}");
echo "<table width=\"100%\" valign=\"top\" border=\"0\" cellspacing=\"1\" cellpadding=\"1\">
<tr>
<td valign=\"top\">File list of {$_SETTINGS['DIR']}/";
echo "<select name=\"edit_file_name\">
<option value=\"$file\">$file</option>
<option value=\"\"></option>";
while ($file = readdir ($dir)) {
if($_SETTINGS['FILE_LIST']){
$file2 = array_pop(explode(".",$file));
if(!in_array($file2,$_SETTINGS['FILE_LIST']))
continue;
}
if ($file != "." && $file != ".." && !is_dir($file) && !eregi("\.bac", $file)){
print '<option value="'.$file.'">'.$file. '</option>';
}
// echo "&nbsp;<a href=\"$_SERVER[PHP_SELF]?edit_file_name=$file\">$file</a></td>&nbsp;\n";
}
echo '</select>&nbsp;&nbsp;&nbsp;';
closedir($dir);
} else echo "{$_SETTINGS['DIR']} not found";
?>



 

enigma1

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4412993 posted 9:09 am on Feb 1, 2012 (gmt 0)

Use the an array to get the filenames first

$files_array = array();
while( ($files_array[] = readdir($dir)) !== false );
array_pop($files_array);
sort($files_array, SORT_STRING);
foreach($files_array as file) {
if($_SETTINGS['FILE_LIST']){
......

or you can do the sorting after you performed filtering and before display it and would be better.

And get rid of the ereg depreciated functions.

chrissim

5+ Year Member



 
Msg#: 4412993 posted 9:32 am on Feb 1, 2012 (gmt 0)

hi,

im getting error here maybe i didn't get it right.

Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM

enigma1

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4412993 posted 10:44 am on Feb 1, 2012 (gmt 0)

You can post the whole code again you use with the mods, at least the part I posted was tested.

chrissim

5+ Year Member



 
Msg#: 4412993 posted 10:53 am on Feb 3, 2012 (gmt 0)

hi guys,

Here is my complete script and i just can't figured out what went wrong and unable to get the dir sorting order. Maybe if you can show me the correct part where it should go.

Thanks


<?
if (is_dir($_SETTINGS['DIR'])) {
$dir = opendir("{$_SETTINGS['DIR']}");
echo "<table width=\"100%\" valign=\"top\" border=\"0\" cellspacing=\"1\" cellpadding=\"1\">
<tr>
<td valign=\"top\"><b>File List Of</b> {$_SETTINGS['DIR']}/";
echo "<select name=\"edit_file_name\">
<option value=\"$file\">$file</option>
<option value=\"\"></option>";
while ($file = readdir ($dir)) {
if($_SETTINGS['FILE_LIST']){
$file2 = array_pop(explode(".",$file));
if(!in_array($file2,$_SETTINGS['FILE_LIST']))
continue;
}

if ($file != "." && $file != ".." && !is_dir($file) && !eregi("\.bac", $file)){
print '<option value="'.$file.'">'.$file. '</option>';
}
// echo "&nbsp;<a href=\"$_SERVER[PHP_SELF]?edit_file_name=$file\">$file</a></td>&nbsp;\n";
}
echo '</select>&nbsp;&nbsp;&nbsp;';
closedir($dir);
} else echo "{$_SETTINGS['DIR']} not found";
?>



enigma1

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4412993 posted 11:36 am on Feb 3, 2012 (gmt 0)

Change this line:

while ($file = readdir ($dir)) {

with this:

$files_array = array();
while( ($files_array[] = readdir($dir)) !== false );
array_pop($files_array);
sort($files_array, SORT_STRING);
foreach($files_array as file) {


and see if it works

chrissim

5+ Year Member



 
Msg#: 4412993 posted 11:59 am on Feb 3, 2012 (gmt 0)

hi enigma1,

i changed it but it don't worked

Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM in /home/#*$!x/public_html/#*$!xx.com

enigma1

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4412993 posted 12:15 pm on Feb 3, 2012 (gmt 0)

oops sorry it should be $file.

change this:
foreach($files_array as file) {

with this:
foreach($files_array as $file) {

chrissim

5+ Year Member



 
Msg#: 4412993 posted 12:40 pm on Feb 3, 2012 (gmt 0)

Great worked like a champ man
Thanks Egnima1 :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved