Guest User

lista

a guest
May 6th, 2012
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.43 KB | None | 0 0
  1. <?php
  2. error_reporting(1);
  3.  
  4. /*******************************************************************************************************************[ SETTINGS ]***/
  5. // Set sorting properties.
  6. $sort = array(
  7.     array('key'=>'lname',   'sort'=>'asc'), // ... this sets the initial sort "column" and order ...
  8.     array('key'=>'size',    'sort'=>'asc') // ... for items with the same initial sort value, sort this way.
  9. );
  10.  
  11. /************************************************************************************************************[ DIRECTORY LOGIC ]***/
  12. // Get this folder and files name.
  13. $this_script = basename(__FILE__);
  14. $this_folder = str_replace('/'.$this_script, '', $_SERVER['SCRIPT_NAME']);
  15.  
  16. // Declare vars used beyond this point.
  17. $file_list = array();
  18. $folder_list = array();
  19. $total_size = 0;
  20. // Open the current directory...
  21. if ($handle = opendir('.'))
  22. {
  23.     // ...start scanning through it.
  24.     while (false !== ($file = readdir($handle)))
  25.     {
  26.         // Make sure we don't list this folder, file or their links.
  27.         if ($file != "." && $file != ".." && $file != $this_script)
  28.         {
  29.             // Get file info.
  30.             $stat               =   stat($file); // ... slow, but faster than using filemtime() & filesize() instead.
  31.             $info               =   pathinfo($file);
  32.             // Organize file info.
  33.             $item['name']       =   $info['filename'];
  34.             $item['lname']      =   strtolower($info['filename']);
  35.             $item['ext']        =   $info['extension'];
  36.                 if($info['extension'] == '') $item['ext'] = '.';
  37.             $item['bytes']      =   $stat['size'];
  38.             $item['size']       =   bytes_to_string($stat['size'], 2);
  39.             $item['mtime']      =   $stat['mtime'];
  40.             // Add files to the file list...
  41.             if($info['extension'] != '')
  42.             {
  43.                 array_push($file_list, $item);
  44.             }
  45.             // ...and folders to the folder list.
  46.             else
  47.             {
  48.                 array_push($folder_list, $item);
  49.             }
  50.             // Clear stat() cache to free up memory (not really needed).
  51.             clearstatcache();
  52.             // Add this items file size to this folders total size
  53.             $total_size += $item['bytes'];
  54.         }
  55.     }
  56.     // Close the directory when finished.
  57.     closedir($handle);
  58. }
  59. // Sort folder list.
  60. if($folder_list)
  61.     $folder_list = php_multisort($folder_list, $sort);
  62. // Sort file list.
  63. if($file_list)
  64.     $file_list = php_multisort($file_list, $sort);
  65. // Calculate the total folder size
  66. if($file_list && $folder_list)
  67.     $total_size = bytes_to_string($total_size, 2);
  68.  
  69.    
  70. /**********************************************************************************************************************************/
  71. /******************************************************************************************************************[ FUNCTIONS ]***/
  72.  
  73. /**
  74.  *  http://us.php.net/manual/en/function.array-multisort.php#83117
  75.  */
  76. function php_multisort($data,$keys)
  77. {
  78.     foreach ($data as $key => $row)
  79.     {
  80.         foreach ($keys as $k)
  81.         {
  82.             $cols[$k['key']][$key] = $row[$k['key']];
  83.         }
  84.     }
  85.     $idkeys = array_keys($data);
  86.     $i=0;
  87.     foreach ($keys as $k)
  88.     {
  89.         if($i>0){$sort.=',';}
  90.         $sort.='$cols['.$k['key'].']';
  91.         if($k['sort']){$sort.=',SORT_'.strtoupper($k['sort']);}
  92.         if($k['type']){$sort.=',SORT_'.strtoupper($k['type']);}
  93.         $i++;
  94.     }
  95.     $sort .= ',$idkeys';
  96.     $sort = 'array_multisort('.$sort.');';
  97.     eval($sort);
  98.     foreach($idkeys as $idkey)
  99.     {
  100.         $result[$idkey]=$data[$idkey];
  101.     }
  102.     return $result;
  103. }
  104.  
  105. /**
  106.  *  @ http://us3.php.net/manual/en/function.filesize.php#84652
  107.  */
  108. function bytes_to_string($size, $precision = 0) {
  109.     $sizes = array('YB', 'ZB', 'EB', 'PB', 'TB', 'GB', 'MB', 'KB', 'Bytes');
  110.     $total = count($sizes);
  111.     while($total-- && $size > 1024) $size /= 1024;
  112.     $return['num'] = round($size, $precision);
  113.     $return['str'] = $sizes[$total];
  114.     return $return;
  115. }
  116.  
  117. /**
  118.  *  @ http://us.php.net/manual/en/function.time.php#71342
  119.  */
  120. function time_ago($timestamp, $recursive = 0)
  121. {
  122.     $current_time = time();
  123.     $difference = $current_time - $timestamp;
  124.     $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
  125.     $lengths = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600);
  126.     for ($val = sizeof($lengths) - 1; ($val >= 0) && (($number = $difference / $lengths[$val]) <= 1); $val--);
  127.     if ($val < 0) $val = 0;
  128.     $new_time = $current_time - ($difference % $lengths[$val]);
  129.     $number = floor($number);
  130.     if($number != 1)
  131.     {
  132.         $periods[$val] .= "s";
  133.     }
  134.     $text = sprintf("%d %s ", $number, $periods[$val]);  
  135.    
  136.     if (($recursive == 1) && ($val >= 1) && (($current_time - $new_time) > 0))
  137.     {
  138.         $text .= time_ago($new_time);
  139.     }
  140.     return $text;
  141. }
  142. ?>
Add Comment
Please, Sign In to add comment