billhobbs Posted October 23, 2018 Share Posted October 23, 2018 (edited) I have a script that retrieves the names of photos in a photo directory. I want to sort them by the names, but my sorting isn't working. Everything seems to work except the order is wrong. My code is below. Any help is appreciated. $path = "../arrangements"; if(isset($_POST['file']) && is_array($_POST['file'])) { foreach($_POST['file'] as $file) { unlink($path . "/" . $file) or die("Failed to <strong class='highlight'>delete</strong> file"); } } $path = "../arrangements"; $dir_handle = @opendir($path) or die("Unable to open folder"); while (false !== ($file = readdir($dir_handle))) { sort($file); if($file == ".") continue; if($file == "..") continue; echo "<img src='../arrangements/$file' alt='$file'><br />"; $filename = substr($file, 0, strrpos($file, ".")); echo $filename; } Edited October 23, 2018 by billhobbs Quote Link to comment Share on other sites More sharing options...
Barand Posted October 23, 2018 Share Posted October 23, 2018 (edited) Store the filenames in an array then sort the array. Try using glob() instead. Edited October 23, 2018 by Barand Quote Link to comment Share on other sites More sharing options...
requinix Posted October 23, 2018 Share Posted October 23, 2018 Of all the PHP 4 and older-style code out there still being spread around the internet, opendir/readdir/closedir must be the most resilient. There is virtually no reason whatsoever to use it anymore, and yet people do. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 23, 2018 Share Posted October 23, 2018 Did you even TRY to debug this yourself? If you had RTFM under 'readdir' you would have found your error in 1 minute. 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.