Reputation: 3
I have this code displaying a list of files but they appear in what seems like a random order, is there anyway to put them in an order eg. last uploaded, last modified or even alphabetical? any help would be very much liked.
<strong><?php echo $lang['post_attach']; ?></strong><br /><br />
<form name="attach" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" ENCTYPE="multipart/form-data">
<input type="hidden" name="action" value="attach_files" />
<input type="hidden" name="form_id" value="<?php echo $form; ?>" />
<select name="files[]" size="10" multiple="true" onChange="javascript:goChkImg(this);">
<?php
// Read the file upload list from the file storage
$handle = opendir(CFG_PARENT."/files");
$i=0; $j=0;
while($filename = readdir($handle))
{
if($filename != "." && $filename != ".." && trim($filename)) {
$file_spec=explode("_", $filename);
if($file_spec[0] == $user) {
echo "<option value=\"{$filename}\">{$filename}</option>";
}
}
}
closedir($handle);
Upvotes: 0
Views: 141
Reputation: 1781
https://www.php.net/manual/en/function.readdir.php states
Returns the name of the next entry in the directory. The entries are returned in the order in which they are stored by the filesystem.
So you could read the information into an array first and sort (https://www.php.net/manual/en/function.sort.php) that or you could use other functions, e.g.
https://www.php.net/manual/en/function.glob.php
which perform sorting.
See the other File functions for accessing other information you may want to sort by:
https://www.php.net/manual/en/ref.filesystem.php
like
https://www.php.net/manual/en/function.stat.php
You could adopt the answers from here:
to include the information from stat().
Upvotes: 1