sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
aRTx's Avatar

How to sort files by Date with this Script?


Question posted by: aRTx (Member) on August 21st, 2008 08:58 AM
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
Expand|Select|Wrap|Line Numbers
  1. <?
  2. /*
  3. ORIGJINALI
  4. Directory Listing Script - Version 2
  5. ====================================
  6. Script Author: Ash Young <ash@evoluted.net>. www.evoluted.net
  7. Layout: Manny <manny@tenka.co.uk>. www.tenka.co.uk
  8.  
  9. REQUIREMENTS
  10. ============
  11. This script requires PHP and GD2 if you wish to use the 
  12. thumbnail functionality.
  13.  
  14. INSTRUCTIONS
  15. ============
  16. 1) Unzip all files 
  17. 2) Edit this file, making sure everything is setup as required.
  18. 3) Upload to server
  19. 4) ??????
  20. 5) Profit!
  21.  
  22. CONFIGURATION
  23. =============
  24. Edit the variables in this section to make the script work as
  25. you require.
  26.  
  27. Start Directory - To list the files contained within the current 
  28. directory enter '.', otherwise enter the path to the directory 
  29. you wish to list. The path must be relative to the current 
  30. directory.
  31. */
  32. $startdir = '.';
  33.  
  34. /*
  35. Show Thumbnails? - Set to true if you wish to use the 
  36. scripts auto-thumbnail generation capabilities.
  37. This requires that GD2 is installed.
  38. */
  39. $showthumbnails = true; 
  40.  
  41. /*
  42. Show Directories - Do you want to make subdirectories available?
  43. If not set this to false
  44. */
  45. $showdirs = true;
  46.  
  47. /* 
  48. Force downloads - Do you want to force people to download the files
  49. rather than viewing them in their browser?
  50. */
  51. $forcedownloads = false;
  52.  
  53. /*
  54. Hide Files - If you wish to hide certain files or directories 
  55. then enter their details here. The values entered are matched
  56. against the file/directory names. If any part of the name 
  57. matches what is entered below then it is now shown.
  58. */
  59. $hide = array(
  60.                 'dlf',
  61.                 'classes',
  62.                 'ikoonid',
  63.                 'images',
  64.                 'indes_icons',
  65.                 'reklama.html',
  66.                 'banneri.html',
  67.                 'banner.jpg',
  68.                 '',
  69.                 '',
  70.                 'mp3.png',
  71.                 'tupanat.jpg',
  72.                 'index.php',
  73.                 'Thumbs',
  74.                 '.htaccess',
  75.                 '.htpasswd'
  76.             );
  77.  
  78. /* 
  79. Show index files - if an index file is found in a directory
  80. to you want to display that rather than the listing output 
  81. from this script?
  82. */            
  83. $displayindex = false;
  84.  
  85. /*
  86. Allow uploads? - If enabled users will be able to upload 
  87. files to any viewable directory. You should really only enable
  88. this if the area this script is in is already password protected.
  89. */
  90. $allowuploads = false;
  91.  
  92. /*
  93. Overwrite files - If a user uploads a file with the same
  94. name as an existing file do you want the existing file
  95. to be overwritten?
  96. */
  97. $overwrite = false;
  98.  
  99. /*
  100. Index files - The follow array contains all the index files
  101. that will be used if $displayindex (above) is set to true.
  102. Feel free to add, delete or alter these
  103. */
  104.  
  105. $indexfiles = array (
  106.                 'index.html',
  107.                 'index.htm',
  108.                 'default.htm',
  109.                 'default.html'
  110.             );
  111.  
  112. /*
  113. File Icons - If you want to add your own special file icons use 
  114. this section below. Each entry relates to the extension of the 
  115. given file, in the form <extension> => <filename>. 
  116. These files must be located within the dlf directory.
  117. */
  118. $filetypes = array (
  119.                 'mp3' => 'mp3.png',
  120.                 'png' => 'jpg.gif',
  121.                 'jpeg' => 'jpg.gif',
  122.                 'bmp' => 'jpg.gif',
  123.                 'jpg' => 'jpg.gif', 
  124.                 'gif' => 'gif.gif',
  125.                 'zip' => 'archive.png',
  126.                 'rar' => 'archive.png',
  127.                 'exe' => 'exe.gif',
  128.                 'setup' => 'setup.gif',
  129.                 'txt' => 'text.png',
  130.                 'htm' => 'html.gif',
  131.                 'html' => 'html.gif',
  132.                 'fla' => 'fla.gif',
  133.                 'swf' => 'swf.gif',
  134.                 'xls' => 'xls.gif',
  135.                 'doc' => 'doc.gif',
  136.                 'sig' => 'sig.gif',
  137.                 'fh10' => 'fh10.gif',
  138.                 'pdf' => 'pdf.gif',
  139.                 'psd' => 'psd.gif',
  140.                 'rm' => 'real.gif',
  141.                 'mpg' => 'video.gif',
  142.                 'mpeg' => 'video.gif',
  143.                 'mov' => 'video2.gif',
  144.                 'avi' => 'video.gif',
  145.                 'eps' => 'eps.gif',
  146.                 'gz' => 'archive.png',
  147.                 'asc' => 'sig.gif',
  148.             );
  149.  
  150. /*
  151. That's it! You are now ready to upload this script to the server.
  152.  
  153. Only edit what is below this line if you are sure that you know what you
  154. are doing!
  155. */
  156. error_reporting(0);
  157. if(!function_exists('imagecreatetruecolor')) $showthumbnails = false;
  158. $leadon = $startdir;
  159. if($leadon=='.') $leadon = '';
  160. if((substr($leadon, -1, 1)!='/') && $leadon!='') $leadon = $leadon . '/';
  161. $startdir = $leadon;
  162.  
  163. if($_GET['dir']) {
  164.     //check this is okay.
  165.  
  166.     if(substr($_GET['dir'], -1, 1)!='/') {
  167.         $_GET['dir'] = $_GET['dir'] . '/';
  168.     }
  169.  
  170.     $dirok = true;
  171.     $dirnames = split('/', $_GET['dir']);
  172.     for($di=0; $di<sizeof($dirnames); $di++) {
  173.  
  174.         if($di<(sizeof($dirnames)-2)) {
  175.             $dotdotdir = $dotdotdir . $dirnames[$di] . '/';
  176.         }
  177.  
  178.         if($dirnames[$di] == '..') {
  179.             $dirok = false;
  180.         }
  181.     }
  182.  
  183.     if(substr($_GET['dir'], 0, 1)=='/') {
  184.         $dirok = false;
  185.     }
  186.  
  187.     if($dirok) {
  188.          $leadon = $leadon . $_GET['dir'];
  189.     }
  190. }
  191.  
  192. if($_GET['download'] && $forcedownloads) {
  193.     $file = str_replace('/', '', $_GET['download']);
  194.     $file = str_replace('..', '', $file);
  195.  
  196.     if(file_exists($leadon . $file)) {
  197.         header("Content-type: application/x-download");
  198.         header("Content-Length: ".filesize($leadon . $file)); 
  199.         header('Content-Disposition: attachment; filename="'.$file.'"');
  200.         readfile($leadon . $file);
  201.         die();
  202.     }
  203. }
  204.  
  205. if($allowuploads && $_FILES['file']) {
  206.     $upload = true;
  207.     if(!$overwrite) {
  208.         if(file_exists($leadon.$_FILES['file']['name'])) {
  209.             $upload = false;
  210.         }
  211.     }
  212.  
  213.     if($upload) {
  214.         move_uploaded_file($_FILES['file']['tmp_name'], $leadon . $_FILES['file']['name']);
  215.     }
  216. }
  217.  
  218. $opendir = $leadon;
  219. if(!$leadon) $opendir = '.';
  220. if(!file_exists($opendir)) {
  221.     $opendir = '.';
  222.     $leadon = $startdir;
  223. }
  224.  
  225. clearstatcache();
  226. if ($handle = opendir($opendir)) {
  227.     while (false !== ($file = readdir($handle))) { 
  228.         //first see if this file is required in the listing
  229.         if ($file == "." || $file == "..")  continue;
  230.         $discard = false;
  231.         for($hi=0;$hi<sizeof($hide);$hi++) {
  232.             if(strpos($file, $hide[$hi])!==false) {
  233.                 $discard = true;
  234.             }
  235.         }
  236.  
  237.         if($discard) continue;
  238.         if (@filetype($leadon.$file) == "dir") {
  239.             if(!$showdirs) continue;
  240.  
  241.             $n++;
  242.             if($_GET['sort']=="date") {
  243.                 $key = @filemtime($leadon.$file) . ".$n";
  244.             }
  245.             else {
  246.                 $key = $n;
  247.             }
  248.             $dirs[$key] = $file . "";
  249.         }
  250.         else {
  251.             $n++;
  252.             if($_GET['sort']=="date") {
  253.                 $key = @filemtime($leadon.$file) . ".$n";
  254.             }
  255.             elseif($_GET['sort']=="size") {
  256.                 $key = @filesize($leadon.$file) . ".$n";
  257.             }
  258.             else {
  259.                 $key = $n;
  260.             }
  261.             $files[$key] = $file;
  262.  
  263.             if($displayindex) {
  264.                 if(in_array(strtolower($file), $indexfiles)) {
  265.                     header("Location: $file");
  266.                     die();
  267.                 }
  268.             }
  269.         }
  270.     }
  271.     closedir($handle); 
  272. }
  273.  
  274. //sort our files
  275. if($_GET['sort']=="date") {
  276.     @ksort($dirs, SORT_NUMERIC);
  277.     @ksort($files, SORT_NUMERIC);
  278. }
  279. elseif($_GET['sort']=="size") {
  280.     @natcasesort($dirs); 
  281.     @ksort($files, SORT_NUMERIC);
  282. }
  283. else {
  284.     @natcasesort($dirs); 
  285.     @natcasesort($files);
  286. }
  287.  
  288. //order correctly
  289. if($_GET['order']=="desc" && $_GET['sort']!="size") {$dirs = @array_reverse($dirs);}
  290. if($_GET['order']=="desc") {$files = @array_reverse($files);}
  291. $dirs = @array_values($dirs); $files = @array_values($files);
  292.  
  293.  
  294. ?>
  295. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  296. <html xmlns="http://www.w3.org/1999/xhtml">
  297. <head>
  298. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  299. <title>Portal Muzikor - www.Tupanat.com <?=dirname($_SERVER['PHP_SELF']).'/'.$leadon;?></title>
  300. <link rel="stylesheet" type="text/css" href="dlf/styles.css" />
  301. <?
  302. if($showthumbnails) {
  303. ?>
  304. <script language="javascript" type="text/javascript">
  305. <!--
  306. function o(n, i) {
  307.     document.images['thumb'+n].src = 'dlf/i.php?f='+i;
  308.  
  309. }
  310.  
  311. function f(n) {
  312.     document.images['thumb'+n].src = 'dlf/trans.gif';
  313. }
  314. //-->
  315. </script>
  316. <?
  317. }
  318. ?>
  319. </head>
  320. <body><center>
  321. <div id="container">
  322.   <h1><img src="dlf/tupanat.jpg" alt="Tupanat.com"></h1>
  323.   <div id="breadcrumbs"> <a href="<?=$_SERVER['PHP_SELF'];?>"><script type="text/javascript"><!--
  324. google_ad_client = "pub-9839513233851256";
  325. /* 336x280, created 8/15/08 */
  326. google_ad_slot = "0215983033";
  327. google_ad_width = 336;
  328. google_ad_height = 280;
  329. //-->
  330. </script>
  331. <script type="text/javascript"
  332. src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
  333. </script>
  334. <script type="text/javascript"><!--
  335. google_ad_client = "pub-9839513233851256";
  336. /* 336x280, created 8/15/08 */
  337. google_ad_slot = "9651077758";
  338. google_ad_width = 336;
  339. google_ad_height = 280;
  340. //-->
  341. </script>
  342. <script type="text/javascript"
  343. src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
  344. </script>
  345. </a> </center>
  346.   <?
  347.       $breadcrumbs = split('/', $leadon);
  348.       if(($bsize = sizeof($breadcrumbs))>0) {
  349.           $sofar = '';
  350.           for($bi=0;$bi<($bsize-1);$bi++) {
  351.             $sofar = $sofar . $breadcrumbs[$bi] . '/';
  352.             echo ' &gt; <a href="'.$_SERVER['PHP_SELF'].'?dir='.urlencode($sofar).'">'.$breadcrumbs[$bi].'</a>';
  353.         }
  354.       }
  355.  
  356.     $baseurl = $_SERVER['PHP_SELF'] . '?dir='.$_GET['dir'] . '&amp;';
  357.     $fileurl = 'sort=name&amp;order=asc';
  358.     $sizeurl = 'sort=size&amp;order=asc';
  359.     $dateurl = 'sort=date&amp;order=asc';
  360.  
  361.     switch ($_GET['sort']) {
  362.         case 'name':
  363.             if($_GET['order']=='asc') $fileurl = 'sort=name&amp;order=desc';
  364.             break;
  365.         case 'size':
  366.             if($_GET['order']=='asc') $sizeurl = 'sort=size&amp;order=desc';
  367.             break;
  368.  
  369.         case 'date':
  370.             if($_GET['order']=='asc') $dateurl = 'sort=date&amp;order=desc';
  371.             break;  
  372.         default:
  373.             $fileurl = 'sort=name&amp;order=desc';
  374.             break;
  375.     }
  376.   ?>
  377.   </div>
  378.   <div id="listingcontainer">
  379.     <div id="listingheader"> 
  380.     <div id="headerfile"><a href="<?=$baseurl . $fileurl;?>">File</a></div>
  381.     <div id="headersize">    <center><a href="<?=$baseurl . $sizeurl;?>">Madhësia</a></center></div>
  382.     <div id="headermodified"><a href="<?=$baseurl . $dateurl;?>">Publikuar</a></div>
  383.     </div>
  384.     <div id="listing">
  385.     <?
  386.     $class = 'b';
  387.     if($dirok) {
  388.     ?>
  389.     <div><a href="<?=$_SERVER['PHP_SELF'].'?dir='.urlencode($dotdotdir);?>" class="<?=$class;?>"><img src="dlf/dirup.png" alt="Folder" /><strong> << Kthehu...</strong> <em>-</em> <?=date ("  ", filemtime($dotdotdir));?></a></div>
  390.     <?
  391.         if($class=='b') $class='w';
  392.         else $class = 'b';
  393.     }
  394.     $arsize = sizeof($dirs);
  395.     for($i=0;$i<$arsize;$i++) {
  396.     ?>
  397.     <div><a href="<?=$_SERVER['PHP_SELF'].'?dir='.urlencode($leadon.$dirs[$i]);?>" class="<?=$class;?>"><img src="dlf/folder.png" alt="<?=$dirs[$i];?>" /><strong><?=$dirs[$i];?></strong> <em>-</em> <?=date ("d m Y", filemtime($leadon.$dirs[$i]));?></a></div>
  398.     <?
  399.         if($class=='b') $class='w';
  400.         else $class = 'b';    
  401.     }
  402.  
  403.     $arsize = sizeof($files);
  404.     for($i=0;$i<$arsize;$i++) {
  405.         $icon = 'unknown.png';
  406.         $ext = strtolower(substr($files[$i], strrpos($files[$i], '.')+1));
  407.         $supportedimages = array('gif', 'png', 'jpeg', 'jpg');
  408.         $thumb = '';
  409.  
  410.         if($showthumbnails && in_array($ext, $supportedimages)) {
  411.             $thumb = '<span><img src="dlf/trans.gif" alt="'.$files[$i].'" name="thumb'.$i.'" /></span>';
  412.             $thumb2 = ' onmouseover="o('.$i.', \''.urlencode($leadon . $files[$i]).'\');" onmouseout="f('.$i.');"';
  413.  
  414.         }
  415.  
  416.         if($filetypes[$ext]) {
  417.             $icon = $filetypes[$ext];
  418.         }
  419.  
  420.         $filename = $files[$i];
  421.         if(strlen($filename)>43) {
  422.             $filename = substr($files[$i], 0, 40) . '...';
  423.         }
  424.  
  425.         $fileurl = $leadon . $files[$i];
  426.         if($forcedownloads) {
  427.             $fileurl = $_SESSION['PHP_SELF'] . '?dir=' . urlencode($leadon) . '&download=' . urlencode($files[$i]);
  428.         }
  429.  
  430.     ?>
  431.     <div><a href="<?=$fileurl;?>" class="<?=$class;?>"<?=$thumb2;?>><img src="dlf/<?=$icon;?>" alt="<?=$files[$i];?>" /><strong><?=$filename;?></strong> <em><?=round(filesize($leadon.$files[$i])/1024);?>KB</em> <?=date ("d m Y", filemtime($leadon.$files[$i]));?><?=$thumb;?></a></div>
  432.     <?
  433.         if($class=='b') $class='w';
  434.         else $class = 'b';    
  435.     }    
  436.     ?></div>
  437.     <?
  438.     if($allowuploads) {
  439.         $phpallowuploads = (bool) ini_get('file_uploads');        
  440.         $phpmaxsize = ini_get('upload_max_filesize');
  441.         $phpmaxsize = trim($phpmaxsize);
  442.         $last = strtolower($phpmaxsize{strlen($phpmaxsize)-1});
  443.         switch($last) {
  444.             case 'g':
  445.                 $phpmaxsize *= 1024;
  446.             case 'm':
  447.                 $phpmaxsize *= 1024;
  448.         }
  449.  
  450.     ?>
  451.     <div id="upload">
  452.         <div id="uploadtitle"><strong>File Upload</strong> (Max Filesize: <?=$phpmaxsize;?>KB)</div>
  453.         <div id="uploadcontent">
  454.             <?
  455.             if($phpallowuploads) {
  456.             ?>
  457.             <form method="post" action="<?=$_SERVER['PHP_SELF'];?>?dir=<?=urlencode($leadon);?>" enctype="multipart/form-data">
  458.             <input type="file" name="file" /> <input type="submit" value="Upload" />
  459.             </form>
  460.             <?
  461.             }
  462.             else {
  463.             ?>
  464.             File uploads are disabled in your php.ini file. Please enable them.
  465.             <?
  466.             }
  467.             ?>
  468.         </div>
  469.  
  470.     </div>
  471.     <?
  472.     }
  473.     ?>
  474.   </div>
  475. </div>
  476. <center> <div> Portal Muzikor - www.Tupanat.com - tupanat@live.com</div></center>
  477. </body>
  478. </html>
3 Answers Posted
aRTx's Avatar
aRTx August 21st, 2008 03:56 PM
Member - 32 Posts
#2: Re: How to sort files by Date with this Script?

How to sort it by DATE?


Expand|Select|Wrap|Line Numbers
  1. //sort our files
  2. if($_GET['sort']=="date") {
  3.     @ksort($dirs, SORT_NUMERIC);
  4.     @ksort($files, SORT_NUMERIC);
  5. }
  6. elseif($_GET['sort']=="size") {
  7.     @natcasesort($dirs); 
  8.     @ksort($files, SORT_NUMERIC);
  9. }
  10. else {
  11.     @natcasesort($dirs); 
  12.     @natcasesort($files);
  13. }
  14.  
  15. //order correctly
  16. if($_GET['order']=="desc" && $_GET['sort']!="size") {$dirs = @array_reverse($dirs);}
  17. if($_GET['order']=="desc") {$files = @array_reverse($files);}
  18. $dirs = @array_values($dirs); $files = @array_values($files);
  19.  
pbmods's Avatar
pbmods August 22nd, 2008 02:05 AM
Site Moderator - 5,290 Posts
#3: Re: How to sort files by Date with this Script?

Heya, Artx.

You'll probably like array_multisort() better (http://php.net/array_multisort).
aRTx's Avatar
aRTx August 22nd, 2008 06:09 PM
Member - 32 Posts
#4: Re: How to sort files by Date with this Script?

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
Reply
Not the answer you were looking for? Post your question . . .
197,049 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,049 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top PHP Contributors