Welcome to WebmasterWorld Guest from 54.162.19.123

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Help listing directory files sorting

     
8:40 am on Feb 1, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:July 24, 2009
posts: 113
votes: 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";
?>


9:09 am on Feb 1, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 30, 2007
posts:1394
votes: 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.
9:32 am on Feb 1, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:July 24, 2009
posts: 113
votes: 0


hi,

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

Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM
10:44 am on Feb 1, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 30, 2007
posts:1394
votes: 0


You can post the whole code again you use with the mods, at least the part I posted was tested.
10:53 am on Feb 3, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:July 24, 2009
posts: 113
votes: 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";
?>


11:36 am on Feb 3, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 30, 2007
posts:1394
votes: 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
11:59 am on Feb 3, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:July 24, 2009
posts: 113
votes: 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
12:15 pm on Feb 3, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 30, 2007
posts:1394
votes: 0


oops sorry it should be $file.

change this:
foreach($files_array as file) {

with this:
foreach($files_array as $file) {
12:40 pm on Feb 3, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:July 24, 2009
posts: 113
votes: 0


Great worked like a champ man
Thanks Egnima1 :)
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members