Jump to content

[SOLVED] Sort Order of File Folder Sharing -- Simple Code Needed! :-)


CookieMonster217

Recommended Posts

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);

?>

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/>";

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.