CookieMonster217 Posted April 21, 2008 Share Posted April 21, 2008 Hello, Below is the code I am using as an index.php file inside a file folder that I want web guests to be able to view the contents of. Everything is working great except that the files are not in the correct alphanumeric order, so I think I need to add a piece of code to tell it to do so, but being a novice at php, I am unsure what to add. (I do have the u/s/e/Username field correct for my file name, so no worries there. Like I said, everything works except sort order.) Could someone look at my code below and tell me what I need to get it to sort my files in ascending alphanumeric order by their file name? What I do is upload all the Word documents to the server through Word and then instead of having to create a link for each document, the files appear right in the folder for viewing on the Internet. Please help! Thanks! :-) <? /** * Change the path to your folder. * * This must be the full path from the root of your * web space. If you're not sure what it is, ask your host. * * Name this file index.php and place in the directory. */ // Define the full path to your folder from root $path = "/home/content/U/s/e/Username/html/Folder/2000/"; // Open the folder $dir_handle = @opendir($path) or die("Unable to open $path"); // Loop through the files while ($file = readdir($dir_handle)) { if($file == "." || $file == ".." || $file == "index.php" ) continue; echo "<a href=\"$file\">$file</a><br />"; } // Close closedir($dir_handle); ?> Link to comment https://forums.phpfreaks.com/topic/102091-solved-sort-order-of-file-folder-sharing-simple-code-needed/ Share on other sites More sharing options...
Barand Posted April 21, 2008 Share Posted April 21, 2008 Store in an array, sort the array, output the files <?php /** * Change the path to your folder. * * This must be the full path from the root of your * web space. If you're not sure what it is, ask your host. * * Name this file index.php and place in the directory. */ // Define the full path to your folder from root $path = "/home/content/U/s/e/Username/html/Folder/2000/"; // Open the folder $dir_handle = @opendir($path) or die("Unable to open $path"); // Loop through the files $files = array(); while ($file = readdir($dir_handle)) { if($file == "." || $file == ".." || $file == "index.php" ) continue; $files[] = $file; // store in an array } // Close closedir($dir_handle); // sort array then output sort ($files); foreach ($files as $file) echo "<a href=\"$file\">$file</a><br/>"; ?> Link to comment https://forums.phpfreaks.com/topic/102091-solved-sort-order-of-file-folder-sharing-simple-code-needed/#findComment-522677 Share on other sites More sharing options...
CookieMonster217 Posted April 22, 2008 Author Share Posted April 22, 2008 WOW! Thank you so much. That extra bit of code is exactly what I needed. I don't know how to thank you, but I am so grateful! THANKS! :-) Link to comment https://forums.phpfreaks.com/topic/102091-solved-sort-order-of-file-folder-sharing-simple-code-needed/#findComment-523702 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.