Posted By

cshaiku on 06/30/09


Tagged

table file directory listing


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

cshaiku
vali29
BrianCoyDesign


Simple File Directory Listing Table


 / Published in: PHP
 

URL: http://code.cshaiku.com/code_php_list_files.php

  1. function listFiles($dir)
  2. {
  3. $output = ''; $outRows = ''; $files = array();
  4. if (is_dir($dir)) {
  5. if ($dirHandle = opendir($dir)) {
  6. $files = array_diff(scandir($dir), array('.', '..', '.htaccess'));
  7. $totalSize = (int) 0;
  8. foreach($files as $file) {
  9. $fileTime = @date("d-M-Y", filectime($dir . '/' . $file)) . ' ' . @date("h:i", filemtime($dir . '/' . $file));
  10. $totalSize += filesize($dir . '/' . $file);
  11. $fileSize = @byte_convert(filesize($dir . '/' . $file));
  12. $cellLink = '<td class="list_files_table_file_link"><a href="/fake/files/' . $file . '">' . $file . '</a></td>';
  13. $cellTime = '<td>' . $fileTime . '</td>';
  14. $cellSize = '<td>' . $fileSize . '</td>';
  15. $outRows .= '<tr>' . "\n " . $cellLink . "\n " . $cellTime . "\n " . $cellSize . "\n" . '</tr>' . "\n";
  16. }
  17. closedir($dirHandle);
  18. }
  19. }
  20. $output = '<table class="list_files_table" width="100%" align="center" cellpadding="3" cellspacing="1" border="0">' . "\n";
  21. $output .= '<thead><tr><td><b>Name</b></td><td><b>Last Modified</b></td><td><b>Size</b></td></tr></thead>' . "\n";
  22. $output .= '<tfoot><tr><td colspan="2">' . count($files) . ' files.</td><td>' . @byte_convert($totalSize) . '</td></tr></tfoot>' . "\n";
  23. $output .= '<tbody>' . "\n";
  24. $output .= $outRows;
  25. $output .= '</body>' . "\n";
  26. $output .= '</table>';
  27.  
  28. return $output;
  29. }
  30.  
  31. function byte_convert($bytes)
  32. {
  33. $symbol = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  34. $exp = (int) 0;
  35. $converted_value = (int) 0;
  36. if ($bytes > 0) {
  37. $exp = floor(log($bytes)/log(1024));
  38. $converted_value = ($bytes/pow(1024,floor($exp)));
  39. }
  40. return sprintf('%.2f ' . $symbol[$exp], $converted_value);
  41. }
  42.  
  43. echo listFiles('/path/to/files');

Report this snippet  

You need to login to post a comment.