473,586 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to sort files by Date with this Script?

30 New Member
I have try a couple of time but does not work for me

My files everytime are sortet by NAME.
I want to Sort my files by Date-desc.
Can anyone help me to do it?

The Script
[PHP]<?
/*
ORIGJINALI
Directory Listing Script - Version 2
=============== =============== ======
Script Author: Ash Young <ash@evoluted.n et>. www.evoluted.ne t
Layout: Manny <manny@tenka.co .uk>. www.tenka.co.uk

REQUIREMENTS
============
This script requires PHP and GD2 if you wish to use the
thumbnail functionality.

INSTRUCTIONS
============
1) Unzip all files
2) Edit this file, making sure everything is setup as required.
3) Upload to server
4) ??????
5) Profit!

CONFIGURATION
=============
Edit the variables in this section to make the script work as
you require.

Start Directory - To list the files contained within the current
directory enter '.', otherwise enter the path to the directory
you wish to list. The path must be relative to the current
directory.
*/
$startdir = '.';

/*
Show Thumbnails? - Set to true if you wish to use the
scripts auto-thumbnail generation capabilities.
This requires that GD2 is installed.
*/
$showthumbnails = true;

/*
Show Directories - Do you want to make subdirectories available?
If not set this to false
*/
$showdirs = true;

/*
Force downloads - Do you want to force people to download the files
rather than viewing them in their browser?
*/
$forcedownloads = false;

/*
Hide Files - If you wish to hide certain files or directories
then enter their details here. The values entered are matched
against the file/directory names. If any part of the name
matches what is entered below then it is now shown.
*/
$hide = array(
'dlf',
'classes',
'ikoonid',
'images',
'indes_icons',
'reklama.html',
'banneri.html',
'banner.jpg',
'',
'',
'mp3.png',
'tupanat.jpg',
'index.php',
'Thumbs',
'.htaccess',
'.htpasswd'
);

/*
Show index files - if an index file is found in a directory
to you want to display that rather than the listing output
from this script?
*/
$displayindex = false;

/*
Allow uploads? - If enabled users will be able to upload
files to any viewable directory. You should really only enable
this if the area this script is in is already password protected.
*/
$allowuploads = false;

/*
Overwrite files - If a user uploads a file with the same
name as an existing file do you want the existing file
to be overwritten?
*/
$overwrite = false;

/*
Index files - The follow array contains all the index files
that will be used if $displayindex (above) is set to true.
Feel free to add, delete or alter these
*/

$indexfiles = array (
'index.html',
'index.htm',
'default.htm',
'default.html'
);

/*
File Icons - If you want to add your own special file icons use
this section below. Each entry relates to the extension of the
given file, in the form <extension> => <filename>.
These files must be located within the dlf directory.
*/
$filetypes = array (
'mp3' => 'mp3.png',
'png' => 'jpg.gif',
'jpeg' => 'jpg.gif',
'bmp' => 'jpg.gif',
'jpg' => 'jpg.gif',
'gif' => 'gif.gif',
'zip' => 'archive.png',
'rar' => 'archive.png',
'exe' => 'exe.gif',
'setup' => 'setup.gif',
'txt' => 'text.png',
'htm' => 'html.gif',
'html' => 'html.gif',
'fla' => 'fla.gif',
'swf' => 'swf.gif',
'xls' => 'xls.gif',
'doc' => 'doc.gif',
'sig' => 'sig.gif',
'fh10' => 'fh10.gif',
'pdf' => 'pdf.gif',
'psd' => 'psd.gif',
'rm' => 'real.gif',
'mpg' => 'video.gif',
'mpeg' => 'video.gif',
'mov' => 'video2.gif',
'avi' => 'video.gif',
'eps' => 'eps.gif',
'gz' => 'archive.png',
'asc' => 'sig.gif',
);

/*
That's it! You are now ready to upload this script to the server.

Only edit what is below this line if you are sure that you know what you
are doing!
*/
error_reporting (0);
if(!function_ex ists('imagecrea tetruecolor')) $showthumbnails = false;
$leadon = $startdir;
if($leadon=='.' ) $leadon = '';
if((substr($lea don, -1, 1)!='/') && $leadon!='') $leadon = $leadon . '/';
$startdir = $leadon;

if($_GET['dir']) {
//check this is okay.

if(substr($_GET['dir'], -1, 1)!='/') {
$_GET['dir'] = $_GET['dir'] . '/';
}

$dirok = true;
$dirnames = split('/', $_GET['dir']);
for($di=0; $di<sizeof($dir names); $di++) {

if($di<(sizeof( $dirnames)-2)) {
$dotdotdir = $dotdotdir . $dirnames[$di] . '/';
}

if($dirnames[$di] == '..') {
$dirok = false;
}
}

if(substr($_GET['dir'], 0, 1)=='/') {
$dirok = false;
}

if($dirok) {
$leadon = $leadon . $_GET['dir'];
}
}

if($_GET['download'] && $forcedownloads ) {
$file = str_replace('/', '', $_GET['download']);
$file = str_replace('.. ', '', $file);

if(file_exists( $leadon . $file)) {
header("Content-type: application/x-download");
header("Content-Length: ".filesize($lea don . $file));
header('Content-Disposition: attachment; filename="'.$fi le.'"');
readfile($leado n . $file);
die();
}
}

if($allowupload s && $_FILES['file']) {
$upload = true;
if(!$overwrite) {
if(file_exists( $leadon.$_FILES['file']['name'])) {
$upload = false;
}
}

if($upload) {
move_uploaded_f ile($_FILES['file']['tmp_name'], $leadon . $_FILES['file']['name']);
}
}

$opendir = $leadon;
if(!$leadon) $opendir = '.';
if(!file_exists ($opendir)) {
$opendir = '.';
$leadon = $startdir;
}

clearstatcache( );
if ($handle = opendir($opendi r)) {
while (false !== ($file = readdir($handle ))) {
//first see if this file is required in the listing
if ($file == "." || $file == "..") continue;
$discard = false;
for($hi=0;$hi<s izeof($hide);$h i++) {
if(strpos($file , $hide[$hi])!==false) {
$discard = true;
}
}

if($discard) continue;
if (@filetype($lea don.$file) == "dir") {
if(!$showdirs) continue;

$n++;
if($_GET['sort']=="date") {
$key = @filemtime($lea don.$file) . ".$n";
}
else {
$key = $n;
}
$dirs[$key] = $file . "";
}
else {
$n++;
if($_GET['sort']=="date") {
$key = @filemtime($lea don.$file) . ".$n";
}
elseif($_GET['sort']=="size") {
$key = @filesize($lead on.$file) . ".$n";
}
else {
$key = $n;
}
$files[$key] = $file;

if($displayinde x) {
if(in_array(str tolower($file), $indexfiles)) {
header("Locatio n: $file");
die();
}
}
}
}
closedir($handl e);
}

//sort our files
if($_GET['sort']=="date") {
@ksort($dirs, SORT_NUMERIC);
@ksort($files, SORT_NUMERIC);
}
elseif($_GET['sort']=="size") {
@natcasesort($d irs);
@ksort($files, SORT_NUMERIC);
}
else {
@natcasesort($d irs);
@natcasesort($f iles);
}

//order correctly
if($_GET['order']=="desc" && $_GET['sort']!="size") {$dirs = @array_reverse( $dirs);}
if($_GET['order']=="desc") {$files = @array_reverse( $files);}
$dirs = @array_values($ dirs); $files = @array_values($ files);


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Portal Muzikor - www.Tupanat.com <?=dirname($_SE RVER['PHP_SELF']).'/'.$leadon;?></title>
<link rel="stylesheet " type="text/css" href="dlf/styles.css" />
<?
if($showthumbna ils) {
?>
<script language="javas cript" type="text/javascript">
<!--
function o(n, i) {
document.images['thumb'+n].src = 'dlf/i.php?f='+i;

}

function f(n) {
document.images['thumb'+n].src = 'dlf/trans.gif';
}
//-->
</script>
<?
}
?>
</head>
<body><center >
<div id="container" >
<h1><img src="dlf/tupanat.jpg" alt="Tupanat.co m"></h1>
<div id="breadcrumbs "> <a href="<?=$_SERV ER['PHP_SELF'];?>"><script type="text/javascript"><!--
google_ad_clien t = "pub-983951323385125 6";
/* 336x280, created 8/15/08 */
google_ad_slot = "0215983033 ";
google_ad_width = 336;
google_ad_heigh t = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googles yndication.com/pagead/show_ads.js">
</script>
<script type="text/javascript"><!--
google_ad_clien t = "pub-983951323385125 6";
/* 336x280, created 8/15/08 */
google_ad_slot = "9651077758 ";
google_ad_width = 336;
google_ad_heigh t = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googles yndication.com/pagead/show_ads.js">
</script>
</a> </center>
<?
$breadcrumbs = split('/', $leadon);
if(($bsize = sizeof($breadcr umbs))>0) {
$sofar = '';
for($bi=0;$bi<( $bsize-1);$bi++) {
$sofar = $sofar . $breadcrumbs[$bi] . '/';
echo ' &gt; <a href="'.$_SERVE R['PHP_SELF'].'?dir='.urlenc ode($sofar).'"> '.$breadcrumbs[$bi].'</a>';
}
}

$baseurl = $_SERVER['PHP_SELF'] . '?dir='.$_GET['dir'] . '&amp;';
$fileurl = 'sort=name&amp; order=asc';
$sizeurl = 'sort=size&amp; order=asc';
$dateurl = 'sort=date&amp; order=asc';

switch ($_GET['sort']) {
case 'name':
if($_GET['order']=='asc') $fileurl = 'sort=name&amp; order=desc';
break;
case 'size':
if($_GET['order']=='asc') $sizeurl = 'sort=size&amp; order=desc';
break;

case 'date':
if($_GET['order']=='asc') $dateurl = 'sort=date&amp; order=desc';
break;
default:
$fileurl = 'sort=name&amp; order=desc';
break;
}
?>
</div>
<div id="listingcont ainer">
<div id="listinghead er">
<div id="headerfile" ><a href="<?=$baseu rl . $fileurl;?>">Fi le</a></div>
<div id="headersize" > <center><a href="<?=$baseu rl . $sizeurl;?>">Ma dhësia</a></center></div>
<div id="headermodif ied"><a href="<?=$baseu rl . $dateurl;?>">Pu blikuar</a></div>
</div>
<div id="listing">
<?
$class = 'b';
if($dirok) {
?>
<div><a href="<?=$_SERV ER['PHP_SELF'].'?dir='.urlenc ode($dotdotdir) ;?>" class="<?=$clas s;?>"><img src="dlf/dirup.png" alt="Folder" /><strong> << Kthehu...</strong> <em>-</em> <?=date (" ", filemtime($dotd otdir));?></a></div>
<?
if($class=='b') $class='w';
else $class = 'b';
}
$arsize = sizeof($dirs);
for($i=0;$i<$ar size;$i++) {
?>
<div><a href="<?=$_SERV ER['PHP_SELF'].'?dir='.urlenc ode($leadon.$di rs[$i]);?>" class="<?=$clas s;?>"><img src="dlf/folder.png" alt="<?=$dirs[$i];?>" /><strong><?=$di rs[$i];?></strong> <em>-</em> <?=date ("d m Y", filemtime($lead on.$dirs[$i]));?></a></div>
<?
if($class=='b') $class='w';
else $class = 'b';
}

$arsize = sizeof($files);
for($i=0;$i<$ar size;$i++) {
$icon = 'unknown.png';
$ext = strtolower(subs tr($files[$i], strrpos($files[$i], '.')+1));
$supportedimage s = array('gif', 'png', 'jpeg', 'jpg');
$thumb = '';

if($showthumbna ils && in_array($ext, $supportedimage s)) {
$thumb = '<span><img src="dlf/trans.gif" alt="'.$files[$i].'" name="thumb'.$i .'" /></span>';
$thumb2 = ' onmouseover="o( '.$i.', \''.urlencode($ leadon . $files[$i]).'\');" onmouseout="f(' .$i.');"';

}

if($filetypes[$ext]) {
$icon = $filetypes[$ext];
}

$filename = $files[$i];
if(strlen($file name)>43) {
$filename = substr($files[$i], 0, 40) . '...';
}

$fileurl = $leadon . $files[$i];
if($forcedownlo ads) {
$fileurl = $_SESSION['PHP_SELF'] . '?dir=' . urlencode($lead on) . '&download=' . urlencode($file s[$i]);
}

?>
<div><a href="<?=$fileu rl;?>" class="<?=$clas s;?>"<?=$thumb2 ;?>><img src="dlf/<?=$icon;?>" alt="<?=$files[$i];?>" /><strong><?=$fi lename;?></strong> <em><?=round(fi lesize($leadon. $files[$i])/1024);?>KB</em> <?=date ("d m Y", filemtime($lead on.$files[$i]));?><?=$thumb; ?></a></div>
<?
if($class=='b') $class='w';
else $class = 'b';
}
?></div>
<?
if($allowupload s) {
$phpallowupload s = (bool) ini_get('file_u ploads');
$phpmaxsize = ini_get('upload _max_filesize') ;
$phpmaxsize = trim($phpmaxsiz e);
$last = strtolower($php maxsize{strlen( $phpmaxsize)-1});
switch($last) {
case 'g':
$phpmaxsize *= 1024;
case 'm':
$phpmaxsize *= 1024;
}

?>
<div id="upload">
<div id="uploadtitle "><strong>F ile Upload</strong> (Max Filesize: <?=$phpmaxsize; ?>KB)</div>
<div id="uploadconte nt">
<?
if($phpallowupl oads) {
?>
<form method="post" action="<?=$_SE RVER['PHP_SELF'];?>?dir=<?=urle ncode($leadon); ?>" enctype="multip art/form-data">
<input type="file" name="file" /> <input type="submit" value="Upload" />
</form>
<?
}
else {
?>
File uploads are disabled in your php.ini file. Please enable them.
<?
}
?>
</div>

</div>
<?
}
?>
</div>
</div>
<center> <div> Portal Muzikor - www.Tupanat.com - tupanat@live.co m</div></center>
</body>
</html>
[/PHP]
Aug 21 '08 #1
3 5173
aRTx
30 New Member
How to sort it by DATE?


[PHP]
//sort our files
if($_GET['sort']=="date") {
@ksort($dirs, SORT_NUMERIC);
@ksort($files, SORT_NUMERIC);
}
elseif($_GET['sort']=="size") {
@natcasesort($d irs);
@ksort($files, SORT_NUMERIC);
}
else {
@natcasesort($d irs);
@natcasesort($f iles);
}

//order correctly
if($_GET['order']=="desc" && $_GET['sort']!="size") {$dirs = @array_reverse( $dirs);}
if($_GET['order']=="desc") {$files = @array_reverse( $files);}
$dirs = @array_values($ dirs); $files = @array_values($ files);

[/PHP]
Aug 21 '08 #2
pbmods
5,821 Recognized Expert Expert
Heya, Artx.

You'll probably like array_multisort () better (http://php.net/array_multisort).
Aug 22 '08 #3
aRTx
30 New Member
in this script are inserted the function to sort files by DATE but how to do it in start of page default sort by DATE
Aug 22 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
13269
by: Raj | last post by:
Hi all, Can anyone help me with a script which would delete files or move them to a different folder at some scheduled time..! Please.....!!! Thanks in advance...
10
3269
by: Colin Steadman | last post by:
I'm a stupid ASP programmer and I dont do Javascript (except for very simple tasks anyway), and I'm in a bit of a predicament. I've used a javascript table sorting script from here: http://www.ipwebdesign.net/kaelisSpace/useful_tableSort.html This works great except it doesn't sort my UK formatted dates properly, and I end up with...
4
20887
by: Jake | last post by:
Im using the following code to display the contents of a directory: <% Set objFso = CreateObject("Scripting.FileSystemObject") Set objFiles = objFso.GetFolder(Server.MapPath(".")) Set fileList = objFiles.Files For Each i in fileList response.write i.name & " - " & i.DateCreated & "<br>" next %>
5
7812
by: SteM | last post by:
Hi!! I need something like a "sortfunction" because i want to sort the files in a directory x by name or date. i can't find any system function in c++ - any ideas, examples?? Thx, SteM
21
3191
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a...
8
1534
by: mesterak | last post by:
I'm trying to write a text log file processor but am having significant performance issues. * On average, there are about 100-200 files to process each file being about 1MB in size. * Typically there are ~600k to 1m lines to process total. Every line in each log file typically contains a date, a time, follwed by a textual message *...
12
3160
by: jonathan184 | last post by:
Hi the purpose of the script I am trying to do is to access a dir and run the ls -l command which will show the files and the modified date, no w the script is to check two files in that dir , if the file has the modified month and day matching today print message log is fine else the log is not updated and thats it. Now the script below I...
3
5442
by: jaeden99 | last post by:
I was wandering if nyone has a script to move files older than x days old? i've seen several to delete, but I don't want to delete. I would like to create a backup of the files first verify with users if it's ok to delete. Thanks in advance. i found one that was really close but it only looks at one folder. I need it to look at files within...
4
2525
by: jonathan184 | last post by:
Hi I have a perl script, basically what it is suppose to do is check a folder with files. Now the files are checked using a timestamp with the command ls -l so the timestamp in this format is checked. Now what the script does is it checks the time stamp and creates a year folder if it does not exist and then creates a month folder if it does not...
1
7965
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1184
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.