Topshed Posted January 11, 2011 Share Posted January 11, 2011 Hi, I have switched to a unix server from a windows one and my dir.php now displays in date order which makes things very hard to find Below is the code I use, can anyone tell me what to put, and where ? to make it sort by file name, or point me to a freeware script that has already invented the wheel so to speak. <?php function humansize($size) { $kb = 1024; // Kilobyte $mb = 1024 * $kb; // Megabyte $gb = 1024 * $mb; // Gigabyte $tb = 1024 * $gb; // Terabyte if($size < $kb) return $size."B"; else if($size < $mb) return round($size/$kb,0)."KB"; else if($size < $gb) return round($size/$mb,0)."MB"; else if($size < $tb) return round($size/$gb,0)."GB"; else return round($size/$tb,2)."TB"; } $path= dirname($_SERVER['SCRIPT_FILENAME']); ?> <h3>Files in <?php print $path; ?>:</h3> <ul> <?php $d = dir($path); $icon = ''; while (false !== ($entry = $d->read())) { if ( substr($entry, 0, 1)=='.' ) continue; $size = filesize($path.'/'.$entry); $humansize = humansize($size); $dotpos = strrpos($entry, '.'); if ($dotpos) { $ext = substr($entry, $dotpos+1); if ($ext === 'jpeg' || $ext === 'gif' || $ext === 'png') { $icon = "<img src='$entry' style='width: 48px; height: auto; vertical-align: text-top;' alt='icon' title='$entry' />"; } } print "<li><a href='$entry'>$entry</a> ($humansize) $icon</li>\n"; $icon= ''; } $d->close(); ?> Please help an old fellow who should know better Regards Topshed Quote Link to comment https://forums.phpfreaks.com/topic/224077-dir-of-unix-server-sorted-by-file-name/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 11, 2011 Share Posted January 11, 2011 You already have a thread for this, where someone replied how you could get the files displayed in the order that you want them to be - http://www.phpfreaks.com/forums/php-coding-help/switch-to-unix-causes-dir-php-problems/ Quote Link to comment https://forums.phpfreaks.com/topic/224077-dir-of-unix-server-sorted-by-file-name/#findComment-1157863 Share on other sites More sharing options...
salathe Posted January 11, 2011 Share Posted January 11, 2011 Use glob() or scandir() which sort alphabetically. If their sorting does not suit, use one of the numerous array sorting functions or even go crazy and use your own sorting algoritm. If you need help, after having a go, then show us where you're stuck at and we can assist. Quote Link to comment https://forums.phpfreaks.com/topic/224077-dir-of-unix-server-sorted-by-file-name/#findComment-1157926 Share on other sites More sharing options...
Topshed Posted January 12, 2011 Author Share Posted January 12, 2011 Thank you for your replies Sorry I knew I had posted it somewhere but I could not find it My problem is I am not able to work out some of these command structures It tried Glob and "Scandir" but with absolutely no effect My latest efforts are with DLF, a system that uses GD2, it works without but the output is scruffy So I now have to get it called correcty Thanks again Topshed Quote Link to comment https://forums.phpfreaks.com/topic/224077-dir-of-unix-server-sorted-by-file-name/#findComment-1158251 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.