How to cook files!

Learn how to cook files like a delicious meal :-)

Starting with a new filesystem

use Filicious\Local\LocalAdapter;
use Filicious\Filesystem;

$adapter    = new LocalAdapter('/var/lib/kitchen');
$filesystem = new Filesystem($adapter);

Move or copy files

Move files

$source = $filesystem->getFile('/source.txt');
$target = $filesystem->getFile('/target.txt');

if (!$target->exists()) {
  $source->moveTo($target);
}
		

Copy files

$source = $filesystem->getFile('/source.txt');
$target = $filesystem->getFile('/target.txt');

if (!$target->exists()) {
  $source->copyTo($target);
}
		

Move directories

$source = $filesystem->getFile('/source/');
$target = $filesystem->getFile('/target/');

if (!$target->exists()) {
  $source->moveTo($target);
}
		

Copy directories

$source = $filesystem->getFile('/source/');
$target = $filesystem->getFile('/target/');

if (!$target->exists()) {
  $source->copyTo($target);
}
		

Accessing the contents of a filesystem

Listing files in a directory

$dir = $filesystem->getFile('/path');

// list directory contents
$files = $dir->ls();
foreach ($files as $file) {
  echo '- ' . $file->getBasename() . PHP_EOL;
}
		

Reading and writing files

$file = $filesystem->getFile('/world');

// write
$file->setContents('Hello world!');

// read
echo $file->getContents(); // outputs "Hello world!"
		

Testing existence and type

Listing files in a directory

$dir = $filesystem->getFile('/path');

// test existence
if (!$dir->exists()) {
  echo 'Path /path does not exists!' . PHP_EOL;
  exit;
}

// tip: you can also iterate over a File object :-)
foreach ($dir as $file) {
  echo '- ' . $file->getBasename();

  // test if $file a symlink
  if ($file->hasPlugin('link') && $file->getPlugin('link')->isLink()) {
    echo ' is a symlink';
  }

  // test if $file a regular file
  else if ($file->isFile()) {
    echo ' is a file';
  }

  // test if $file a directory
  else if ($file->isDirectory()) {
    echo ' is a directory';
  }

  echo PHP_EOL;
}

Searching files

The methods File::ls(), File::count() and File::getIterator() accept different filter arguments:

$dir = $filesystem->getFile('/path');

// count files recursive
echo $dir->count(File::LIST_RECURSIVE) . ' files and directories found' . PHP_EOL;

// find all TXT files inside of the directory
foreach ($dir->ls('*.txt') as $file) {
  echo '- ' . $file->getBasename() . PHP_EOL;
}

// find images by mime type
$iterator = $dir->getIterator(
  function(File $file) {
    if ($file->isFile() && $file->hasPlugin('mime')) {
      $mime = $file->getPlugin('mime');
      return preg_match('~^image/', $mime->getMimeType());
    }

    return false;
  }
);
foreach ($iterator as $file) {
  echo '- ' . $file->getBasename() . PHP_EOL;
}

You can also combine any filter.

$dir = $filesystem->getFile('/path');

$files = $dir->ls(
  File::LIST_RECURSIVE,
  '*/images/*',
  function(File $file) {
    if ($file->isFile() && $file->hasPlugin('mime')) {
      $mime = $file->getPlugin('mime');
      return preg_match('~^image/', $mime->getMimeType());
    }

    return false;
  }
);

foreach ($files as $file) {
  echo '- ' . $file->getBasename() . PHP_EOL;
}

Filename vs. pathname globs

There are two types of glob patterns. Filename globs are applied on the filename (File::getBasename()) only. Pathname globs are applied on the pathname (File::getPathname()) only.

The decision of glob type is simple. Filename globs does not contain a /, e.g. *.txt. Pathname globs contains at least one /, e.g. */images/*.