astarmathsandphysics Posted November 19, 2008 Share Posted November 19, 2008 How do I edit this file to make directories list in alphabetical order? <?php /*The following are for styling only*/ echo "<html><head><link rel=stylesheet href=links.css /><title>Free Exam Papers</title></head>"; echo "<body>"; include("upper.title"); echo "<br>"; include ("DirUpper.ads"); /*The styling finishes up*/ ############ FUNCTIONS ############# function cScanDir($dir){ if($handle = opendir($dir)){ while(false !==($file=readdir($handle))){ if($file !="."&&$file !="..") $file_temp['name']=$file; $file=$dir.'/'.$file; $file_temp['type']=(is_dir($file) ? 'Folder' : fileType($file)); $file_temp['time']=date("d/m/Y - G:i:s", filectime($file)); $file_temp['size']=round((filesize($file)/(filesize($file)>1048576 ? 1048576 : (filesize($file)> 1024 ? 1024 : 1))),2).''.(filesize($file)>1048576 ? 'mb':(filesize($file)>1024 ? 'kb': (filesize($file)== 0 ? "" :'b'))); //i did some change some things as i thought they were bugs. $files[]=$file_temp; unset($file_temp); } closedir($handle); } return $files; //###########I CHANGED THIS VARIABLE###########// } function CheckDirectroyFiles(){ $defDir='IGCSE'; if($_GET["d"]!=""){ $dir=$_GET["d"]; $dir=base64_decode($dir); } else { $dir=$defDir; } $files=cScanDir($dir); $numfiles=count($files); //showing files start here echo "<hr>"; /* No pagination shit! if($dir != $defdir){ $dir_list=explode('/',$dir); foreach($dir_list as $dir_temp){ $dir_run.=($dir_run ? '/':"").$dir_temp; $paginate[]='<a href='.$SERVER['PHP_SELF'].'?d='.$dir_run.'>'.$dir_temp.'</a>'; } unset($dir_temp,$dir_list,$dir_run); echo 'Parent directory:'.implode('/',$paginat); }*/ echo "<hr><table>"; echo "<tr><td width=400><u>File</u></td><td width=80><u>Size</u></td><td width=150><u>Last Modified</u></td><td width=100><u>File Type</u></td></tr>"; echo "</table><hr color=#AE4600><table>"; $thePHPFilename=$_SERVER["PHP_self"]; $currentDir="a/"; // change this directory when put in server you can change this var, might not even need it! for($a=0;$a<$numfiles;$a++){ if($files[$a]['type']=='Folder'){ /* $href=$_SERVER['PHP_SELF'].'?d='.$files[$a];//i have changed this bit to make it more sensible*/ $foldernamin=$files[$a]['name']; $stuffers=$dir.'/'.$foldernamin; $URLEncodinDirectory=base64_encode("$stuffers"); $scriptFileToUse=$thePHPFilename.'?d='.$URLEncodinDirectory; } else{ $filenamin=$files[$a]['name']; $stuffs=$dir.'/'.$filenamin; $URLEncodinFiles=base64_encode("$stuffs"); $scriptFileToUse='Download.php?f='.$URLEncodinFiles; /*$href='download.php?f='$dir.$files[$a]['name']; */ } echo '<tr><td width=400><a href='.$scriptFileToUse.'>'.$files[$a]['name'].'</a></td><td width=80>'.$files[$a]['size'].'</td><td width=150>'.$files[$a]['time'].'</td><td width=100>'.$files[$a]['type'].'<td></tr>'; } echo "</table>"; echo "<hr>"; } //directory problem solved here! ##########END FUNCTIONS################ echo "<table width=100%><tr><td width=130px>"; include("leftH.ads"); echo "</td><td>"; CheckDirectroyFiles(); echo "</td><td width=130px>"; include("rightH.ads"); echo "</td></tr></table>"; echo "<br>"; include ("DirLower.ads"); echo "<hr>"; include("bottom.footer"); echo "</body>"; echo "</html>" ?> EDITED BY WILDTEEN88: Please use code tags ( ) when posting code [/code] Link to comment https://forums.phpfreaks.com/topic/133381-making-directories-list-in-alphabetical-order/ Share on other sites More sharing options...
CroNiX Posted November 19, 2008 Share Posted November 19, 2008 sort($your_array); Link to comment https://forums.phpfreaks.com/topic/133381-making-directories-list-in-alphabetical-order/#findComment-693731 Share on other sites More sharing options...
astarmathsandphysics Posted November 19, 2008 Author Share Posted November 19, 2008 I am totally new to php, and dont uderstand anything about arrays. I really want an example to study. Link to comment https://forums.phpfreaks.com/topic/133381-making-directories-list-in-alphabetical-order/#findComment-693737 Share on other sites More sharing options...
premiso Posted November 19, 2008 Share Posted November 19, 2008 Untested. I re-created the function to accept a new parameter "alpha" which will return either an alphabetized list or non-alpahbetized list. <?php function cScanDir($dir, $alpha=false){ if($handle = opendir($dir)){ while(false !==($file=readdir($handle))){ if($file !="."&&$file !="..") $file_temp['name']=$file; $file=$dir.'/'.$file; $file_temp['type']=(is_dir($file) ? 'Folder' : fileType($file)); $file_temp['time']=date("d/m/Y - G:i:s", filectime($file)); $file_temp['size']=round((filesize($file)/(filesize($file)>1048576 ? 1048576 : (filesize($file)> 1024 ? 1024 : 1))),2).''.(filesize($file)>1048576 ? 'mb':(filesize($file)>1024 ? 'kb': (filesize($file)== 0 ? "" :'b'))); //i did some change some things as i thought they were bugs. $files[$file]=$file_temp; unset($file_temp); } closedir($handle); } if ($alpha) { sort($files); // will sort the files by name. // if you want to main regular index recreate the array. foreach ($files as $file) { $newFiles[] = $file; } } return ($alpha)?$newFiles:$files; //###########I CHANGED THIS VARIABLE###########// } ?> Should work like you want it to, untested. Link to comment https://forums.phpfreaks.com/topic/133381-making-directories-list-in-alphabetical-order/#findComment-693741 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.