memoishin's avatar

Laravel File equivalent in Lumen

I can access the list of files in a directory in Laravel using this:

use File; .... $files = File::files($path);

However, I get this error in lumen:

Class 'File' not found

Any idea how I can access list of files in a folder in lumen.

0 likes
2 replies
robrogers3's avatar

you can use SplFile. Which is what is what Larvel uses.

  use \DirectoryIterator;

  function getFileList($dir)
  {
    // array to hold return value
    $retval = [];

    // add trailing slash if missing
    if(substr($dir, -1) != "/") $dir .= "/";

    // open directory for reading
    $d = new DirectoryIterator($dir) or die("getFileList: Failed opening directory $dir for reading");
    foreach($d as $fileinfo) {
      // skip hidden files
      if($fileinfo->isDot()) continue;
      $retval[] = [
        'name' => "{$dir}{$fileinfo}",
        'type' => ($fileinfo->getType() == "dir") ? "dir" : mime_content_type($fileinfo->getRealPath()),
        'size' => $fileinfo->getSize(),
        'lastmod' => $fileinfo->getMTime()
      ];
    }

    return $retval;
  }


$files = getFileList("images");
1 like

Please or to participate in this conversation.