View Full Version : Sorting file names by date

11-16-2011, 11:56 AM
Hi everyone,

I am having a little issue with sorting file names in a drop down box by date. At the moment the files are arranged in a random manor and its not easy trying to find the right file you are looking for.

We have a website that was designed and coded by a company that no longer exists so they can't help us any longer. The backend of the site is designed to manage products, create/delete and etc etc. Before, when we went to add a new product, it would give us a dropdown box with all the images on the server sorted by date, now since we moved servers, it no longer does this.

So I was wondering if there is something I need to do or maybe change in the coding?

This is the code snippet:

$sServerDir = GetRootPath() . $Config['ProductHiresFilesPath'];
@$oCurrentFolder = opendir( $sServerDir ) ;
while ( @$sFile = readdir( $oCurrentFolder ) )
if ( $sFile != '.' && $sFile != '..' && !is_dir( $sServerDir . '/' . $sFile ) )
echo "
<option value='$sFile' ".(($row->product_hires_file == $sFile)?"selected='selected'":'').">$sFile</option>\n";

Any help is appreciated!

11-16-2011, 03:08 PM
Try echoing this statement in your code

$sServerDir = GetRootPath() . $Config['ProductHiresFilesPath'];
echo $sServerDir;

and make sure that the path matches the path that your files are in. If you changed the name of your folders, that may be an issue.

If it doesn't output the correct url ( or nothing at all), get rid of GetRootPath() and try this:

$sServerDir = $_SERVER['HTTP_HOST'] . $Config['ProductHiresFilesPath'];
echo $sServerDir;

There may also be issues with the $Config (Array of file paths?). But try those steps and let me know what you run into.

11-16-2011, 03:42 PM
Thanks for reply,

Unfortunately that did nothing. The drop-down box still sorts the items randomly and I we need them by date of upload which would make the zip file easier to find.

See the attached image please. Would you like the code for the whole file?

11-16-2011, 04:59 PM
Take the part you need out of it, here is a complete one.


/* Rendering */
function list_dir($path)
$items = get_sorted_entries($path);

if (!$items)

foreach($items as $item)
if ($item->type=='dir')
echo ("<li><img alt='PDF' src='../../../images/Folder.png'>".$item->entry."
echo "<ul>\n";
echo "<img alt='PDF' src='../../../images/PDF.png'> ".$item->entry."<br/>";

echo "</ul>";
echo "</li>\n";


/* Finding */
function get_sorted_entries($path)
$dir_handle = @opendir($path) ;
$items = array();

while (false !== ($item = readdir($dir_handle)))
$dir =$path.'/'.$item;
if ( $item == '.' || $item =='..' )

$items[] = (object) array('type'=>'dir','entry'=>$item, 'full_path'=>$dir);
$items[] = (object) array('type'=>'file','entry'=>$item, 'full_path'=>$dir);


return $items;

/* Sorting */
function _sort_entries($a, $B)
return strcmp($a->entry,$b->entry);
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>

<link rel="stylesheet" href="../jquery.treeview.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>

<script src="../lib/jquery.cookie.js" type="text/javascript"></script>
<script src="../jquery.treeview.js" type="text/javascript"></script>

<script type="text/javascript" src="demo.js"></script>


<div id="treecontrol">
<a title="Collapse the entire tree below" href="#"> Collapse All</a> |
<a title="Expand the entire tree below" href="#"> Expand All</a> |
<a title="Toggle the tree below, opening closed branches, closing open branches" href="#">
Toggle All</a>

<ul id="red" class="treeview-red">
list_dir("directory you want to search");

<hr />


11-16-2011, 06:24 PM
I am not sure which bit as I am not fluent in php! :(