Skip to content

FileFinder

Mistralys edited this page Apr 29, 2021 · 2 revisions

For a more flexible way to search for files than the FileFinder::findFiles() method, you can use the file finder class.

Simply create an instance of it with the existing factory method:

use AppUtils;

$finder = FileHelper::createFileFinder('/path/to/folder');

Setting up the finder is easy to do, provided your development IDE provides function insight.

Examples

Fetching all TXT files in a folder

use AppUtils;

$files = FileHelper::createFileFinder('/path/to/folder')
  ->includeExtension('txt')
  ->getAll();

Fetching all TXT files recursively

This will fetch all .txt files in the target folder and all subfolders, with absolute paths.

use AppUtils;

$files = FileHelper::createFileFinder('/path/to/folder')
  ->includeExtension('txt')
  ->makeRecursive()
  ->getAll();

Fetching files with relative paths

This will fetch all files from the folder and its subfolders, returning paths relative to the target folder.

use AppUtils;

$files = FileHelper::createFileFinder('/path/to/folder')
  ->makeRecursive()
  ->setPathmodeRelative()
  ->getAll();

Fetching file names without extension

This will fetch only the file names of all files in the folder.

use AppUtils;

$files = FileHelper::createFileFinder('/path/to/folder')
  ->stripExtensions() // remove extensions
  ->setPathmodeStrip() // remove the path
  ->getAll();

Exclude files by their extension

This will fetch all files from the folder, excluding PDF files.

use AppUtils;

$files = FileHelper::createFileFinder('/path/to/folder')
  ->excludeExtension('pdf')
  ->getAll();

New here?

Have a look at the overview for a list of all helper classes available in the package.

Table of contents

Find the current page in the collapsible "Pages" list above, and expand the page, to view a table of contents.

Clone this wiki locally