Tutorial

Cyril edited this page May 26, 2016 · 4 revisions

This page provides an overview of vfsStream functionality.

The full machine generated documentation is available at http://vfs.bovigo.org/apidoc/.

Initializing

You start using vfsStream by creating a (virtual) root directory:

$root = vfsStream::setup('home');

All files created in vfsStream will be below the specified directory, i.e. home/sample.txt or home/dir1 and home/dir1/input.csv.

Creating a file

You can now manipulate files using all the standard PHP functions. To do this, you'll need a filename. That is where vfsStream::url() is used. Thus to create a file you can:

vfsStream::setup('home');
$file = vfsStream::url('home/test.txt');
file_put_contents($file, "The new contents of the file");

However, you can also create a file directly using vfsStream as follows:

$root = vfsStream::setup('home');
vfsStream::newFile('test.txt')->at($root)->setContent("The new contents of the file");

The newFile method creates a file but doesn't place it into the file system. The chained at method specifies where in the file system to place it (i.e. /home/test.txt). Finally, the setContent method initializes the file with data in the provided string.