PHP 41
FileList Guest on 31st July 2020 05:34:32 PM
  1. <?PHP
  2.   function getFileList($dir)
  3.   {
  4.  
  5.     if(function_exists('mime_content_type'))
  6.       $finfo = FALSE;
  7.     } else {
  8.       $finfo = finfo_open(FILEINFO_MIME_TYPE);
  9.     }
  10.  
  11.     // array to hold return value
  12.     $retval = [];
  13.  
  14.     // add trailing slash if missing
  15.     if(substr($dir, -1) != "/") {
  16.       $dir .= "/";
  17.     }
  18.  
  19.     // open pointer to directory and read list of files
  20.     $d = @dir($dir) or die("getFileList: Failed opening directory {$dir} for reading");
  21.     while(FALSE !== ($entry = $d->read())) {
  22.       // skip hidden files
  23.       if($entry{0} == ".") continue;
  24.       if(is_dir("{$dir}{$entry}")) {
  25.         $retval[] = [
  26.           'name' => "{$dir}{$entry}/",
  27.           'type' => filetype("{$dir}{$entry}"),
  28.           'size' => 0,
  29.           'lastmod' => filemtime("{$dir}{$entry}")
  30.         ];
  31.       } elseif(is_readable("{$dir}{$entry}")) {
  32.         $retval[] = [
  33.           'name' => "{$dir}{$entry}",
  34.           'type' => ($finfo) ? finfo_file($finfo, "{$dir}{$entry}") : mime_content_type("{$dir}{$entry}"),
  35.           'size' => filesize("{$dir}{$entry}"),
  36.           'lastmod' => filemtime("{$dir}{$entry}")
  37.         ];
  38.       }
  39.     }
  40.     $d->close();
  41.  
  42.     return $retval;
  43.   }
  44. ?>

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.