Topshed Posted January 5, 2011 Share Posted January 5, 2011 Hi, I am in the process of switching over to a unix server, and I found the dir program I use in development now sorts the unix way by date ! MySQL client version 5.1.52 PHP latest Below in my script how do I change the output to sort by name please <html> <head> <title>DIR</title> </head> <link rel="stylesheet" type="text/css" href="../../css/tableLL.css"> <body> <?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"; } // get local directory path $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; // get size $size = filesize($path.'/'.$entry); $humansize = humansize($size); // find filename extension $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(); ?> </ul> <hr width="100%"> I do hope someone can help Regards Topshed Quote Link to comment https://forums.phpfreaks.com/topic/223454-switch-to-unix-causes-dirphp-problems/ Share on other sites More sharing options...
unlishema.wolf Posted January 5, 2011 Share Posted January 5, 2011 I will try to help. However I have a slight problem. I'm using my android and I can't read your code. If you can attach it as a text file I can download it and read it. Or something so it isn't in a code block because I can't read code blocks. Quote Link to comment https://forums.phpfreaks.com/topic/223454-switch-to-unix-causes-dirphp-problems/#findComment-1155119 Share on other sites More sharing options...
Topshed Posted January 5, 2011 Author Share Posted January 5, 2011 Thank you for your reply attached is a cleaned up version, with comments and extra spacing removed Thanks Topshed [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/223454-switch-to-unix-causes-dirphp-problems/#findComment-1155460 Share on other sites More sharing options...
unlishema.wolf Posted January 6, 2011 Share Posted January 6, 2011 okay so what you are saying is that instead of displaying the dir while the files are in, it displays the date of the dir? and i got internet on my pc for now. hopefully i keep it. thanks to my phone. Quote Link to comment https://forums.phpfreaks.com/topic/223454-switch-to-unix-causes-dirphp-problems/#findComment-1155532 Share on other sites More sharing options...
PFMaBiSmAd Posted January 6, 2011 Share Posted January 6, 2011 The php.net documentation only states the order in which directory entries are returned by the read method is system-dependent. It is likely that the order is that which the entries are stored in the directory structure. If you want your files to be listed in a specific order, you should read the names into an array and sort that array the way you want. You many find that using a function like glob would be more suitable as it returns file names sorted alphabetically by default. Quote Link to comment https://forums.phpfreaks.com/topic/223454-switch-to-unix-causes-dirphp-problems/#findComment-1155538 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.