prakash Posted August 31, 2008 Share Posted August 31, 2008 I have files (1.jpg, 2.jpg, 3.jpg ....... 10.jpg, 11.jpg) inside one dir and (a.jpg, b.jpg ....... i.jpg .......... z.jpg) in another dir. I created following code for files listing <?php $dir=opendir('dir1'); while (false !== ($file = readdir($dir))) { print $file."<br>"; } closedir($dir); ?> same code works on my local pc and list all files alphabetically or numerically, but when I upload it online it doesn't list out files alphabetically or numerically. could anyone help me? Quote Link to comment https://forums.phpfreaks.com/topic/122079-readdir-alphabetically-and-numerically/ Share on other sites More sharing options...
HeaDmiLe Posted August 31, 2008 Share Posted August 31, 2008 Is acceptable to put names into array and sort it? Quote Link to comment https://forums.phpfreaks.com/topic/122079-readdir-alphabetically-and-numerically/#findComment-630265 Share on other sites More sharing options...
prakash Posted August 31, 2008 Author Share Posted August 31, 2008 isn't there any other way for sorting it rather than creating array and sorting it? Quote Link to comment https://forums.phpfreaks.com/topic/122079-readdir-alphabetically-and-numerically/#findComment-630267 Share on other sites More sharing options...
HeaDmiLe Posted August 31, 2008 Share Posted August 31, 2008 There must be sth less brutal than putting it into array and sorting... Did you try to get filenames by the instance of Directory class? http://www.php.net/manual/en/class.dir.php Quote Link to comment https://forums.phpfreaks.com/topic/122079-readdir-alphabetically-and-numerically/#findComment-630268 Share on other sites More sharing options...
prakash Posted August 31, 2008 Author Share Posted August 31, 2008 There must be sth less brutal than putting it into array and sorting... Did you try to get filenames by the instance of Directory class? http://www.php.net/manual/en/class.dir.php it outputs files in same order here is the test link Quote Link to comment https://forums.phpfreaks.com/topic/122079-readdir-alphabetically-and-numerically/#findComment-630270 Share on other sites More sharing options...
HeaDmiLe Posted August 31, 2008 Share Posted August 31, 2008 I suggest you to do it in brutal way until you're searhing for the source of problem. Quote Link to comment https://forums.phpfreaks.com/topic/122079-readdir-alphabetically-and-numerically/#findComment-630279 Share on other sites More sharing options...
kenrbnsn Posted August 31, 2008 Share Posted August 31, 2008 You should look at the glob() function. It returns a sorted array. Ken Quote Link to comment https://forums.phpfreaks.com/topic/122079-readdir-alphabetically-and-numerically/#findComment-630321 Share on other sites More sharing options...
richardw Posted August 31, 2008 Share Posted August 31, 2008 check out this solution by Barand: http://www.php-editors.com/forums/php-programming-help/2389-need-help-sorting-directory-files.html Quote Link to comment https://forums.phpfreaks.com/topic/122079-readdir-alphabetically-and-numerically/#findComment-630326 Share on other sites More sharing options...
DarkWater Posted August 31, 2008 Share Posted August 31, 2008 You should look at the glob() function. It returns a sorted array. Ken This is probably the preferred method regardless of whether or not you need it sorted. xD Quote Link to comment https://forums.phpfreaks.com/topic/122079-readdir-alphabetically-and-numerically/#findComment-630327 Share on other sites More sharing options...
HeaDmiLe Posted August 31, 2008 Share Posted August 31, 2008 Problem solved, right? Nice, I learned sth new... This is great community Quote Link to comment https://forums.phpfreaks.com/topic/122079-readdir-alphabetically-and-numerically/#findComment-630334 Share on other sites More sharing options...
PFMaBiSmAd Posted August 31, 2008 Share Posted August 31, 2008 To get file names like 2.jpg, 10.jpg to sort correctly, you need to use natsort(). Either use the code at the link richardw provided, and change sort() to natsort() or simply use glob() followed by a natsort(). Quote Link to comment https://forums.phpfreaks.com/topic/122079-readdir-alphabetically-and-numerically/#findComment-630336 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.