Simple File Directory Listing Table


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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');

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.