tigertim Posted August 25, 2010 Share Posted August 25, 2010 I'm pretty new to php and I'm furiously trying to get this code to list the folders/files alphabetically but to no avail! I'd really appreciate it if someone could take a look and guide me as to what to do, I'm pulling what little hair i have left out! I can currently see the folder contents but the order is all over the place. Thanks very much!!! -------------------------- <?php $sub = ($_GET['dir']); if (strstr($sub, "..")) {$sub = "";} $path = '../media/Movies'; $path = $path . "$sub"; $dh = opendir($path); $i=1; while (($file = readdir($dh)) !== false) { if(substr($file,0,1) != ".") { if (substr($file, -4, -3) =="."){ echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. $file <br />"; }else{ echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. <a href='?dir=$sub/$file'>$file</a><br />"; } $i++; } } closedir($dh); ?> -------------------------- Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 25, 2010 Share Posted August 25, 2010 Instead of opendir() try glob(), then use sort() on the resulting array. You can then loop through the array and do your echos. Also, instead of the substr() calls, maybe use is_dir() and is_file(). Quote Link to comment Share on other sites More sharing options...
tigertim Posted August 25, 2010 Author Share Posted August 25, 2010 Thanks very much for quick reply! As I said I'm pretty new to PHP so please bear with me. Taking your advice, should the code therefore look like this (I'm guessing not as it doesn't seem to work) :'( : -------------------------- <?php $sub = ($_GET['dir']); if (strstr($sub, "..")) {$sub = "";} $path = '../media/Movies'; $path = $path . "$sub"; $dh = glob($path); array = sort(a,z) $i=1; while (($file = readdir($dh)) !== false) { if(substr($file,0,1) != ".") { if (substr($file, -4, -3) =="."){ echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. $file <br />"; }else{ echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. <a href='?dir=$sub/$file'>$file</a><br />"; } $i++; } } closedir($dh); ?> -------------------------- Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 25, 2010 Share Posted August 25, 2010 Did you even look up the functions and how to use them with examples? $files = glob($path); sort($files); foreach($files as $file) { echo $file; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.