This version is outdated. You should upgrade your project to Mako 12.1!
Learn more

File system



The phpFileSystem class contains methods that assist in working with the file system.


Usage

You can create a new FileSystem object or fetch the instance present in the IoC container. In the following examples we'll assume that you'll using the instance from the container.

The phpexists method return TRUE if the provided path exists and FALSE if not.

$exists = $this->fileSystem->exists('/foo/bar.txt');

The phpisFile method return TRUE if the provided path is a file and FALSE if not.

$isFile = $this->fileSystem->isFile('/foo/bar.txt');

The phpisDirectory method returns TRUE if the provided path is a directory and FALSE it not.

$isDirectory = $this->fileSystem->isDirectory('/foo');

The phpisReadable method returns TRUE if the provided path is readable and FALSE if not.

$isReadable = $this->fileSystem->isReadable('/foo/bar.txt');

The phpisWritable method returns TRUE if the provided path is writable and FALSE if not.

$isWritable = $this->fileSystem->isWritable('/foo/bar.txt');

The phplastModified method returs the time (unix timestamp) when the data blocks of a file were being written to, that is, the time when the content of the file was changed.

$lastModified = $this->fileSystem->lastModified('/foo/bar.txt');

The phpsize method returns the size of the file in bytes.

$size = $this->fileSystem->size('/foo/bar.txt');

The phpextension method returns the extension of the file.

$extension = $this->fileSystem->extension('/foo/bar.txt');

The phpmime method returns the mime type of the file. It returns FALSE if the mime type is not found.

$mime = $this->fileSystem->mime('/foo/bar.txt');

The method will try to guess the mimetype by using the file extension if the finfo_open() function doesn't exist. Note that this is not a very reliable way of determinating a mime type. You can disable guessing by setting the second parameter to FALSE.

The phpdelete method will delete a file from disk.

$this->fileSystem->delete('/foo/bar.txt');

The phpglob method returns an array of path names matching the provided pattern.

$paths = $this->fileSystem->glob('/foo/*.txt');

The phpgetContents method returns the contents of a file.

$contents = $this->fileSystem->getContents('/foo/bar.txt');

The phpputContents method puts the provided contents to the file. There's an optional third parameter that will set an exclusive write lock if set to TRUE.

$this->fileSystem->putContents('/foo/bar.txt', 'hello, world!');

The phpprependContents method will prepend the provided conetents to the file. There's an optional third parameter that will set an exclusive write lock if set to TRUE.

$this->fileSystem->prependContents('/foo/bar.txt', 'hello, world!');

The phpappendContents method will append the provided conetents to the file. There's an optional third parameter that will set an exclusive write lock if set to TRUE.

$this->fileSystem->appendContents('/foo/bar.txt', 'hello, world!');

The phptruncateContents method will truncate the contents of the file. There's an optional third parameter that will set an exclusive write lock if set to TRUE.

$this->fileSystem->truncateContents('/foo/bar.txt');

The phpincludeFile method will include a file.

$this->fileSystem->includeFile('/foo/bar.txt');

The phpincludeFileOnce method will include a file if it hasn't already been included.

$this->fileSystem->includeFileOnce('/foo/bar.txt');

The phprequireFile method will require a file.

$this->fileSystem->requireFile('/foo/bar.txt');

The phprequireFileOnce method will require a file if it hasn't already been required.

$this->fileSystem->requireFileOnce('/foo/bar.txt');

The phpfile method will return a SplFileObject.

$file = $this->fileSystem->file('/foo/bar.txt', 'r');