Getting started
Routing and controllers
Command line
Databases (SQL)
Databases (NoSQL)
Security
Packages
Learn more
- Array helper
- Caching
- Command bus
- Date and time
- Events
- File system
- HTML helper
- Humanizer
- Image manipulation
- Internationalization
- Logging
- Number helper
- Pagination
- Sessions
- String helper
- URL builder
- UUID helper
- Validation
- Views
Official packages
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 phphas method return TRUE if the provided path exists and FALSE if not.
$exists = $this->fileSystem->has('/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 if not.
$isDirectory = $this->fileSystem->isDirectory('/foo');
The phpisEmpty method returns TRUE if the provided path is an empty file or directory and FALSE if not.
$isEmpty = $this->fileSystem->isEmpty('/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 returns 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 determining a mime type. You can disable guessing by setting the second parameter to FALSE.
The phpremove method will delete a file from disk.
$this->fileSystem->remove('/foo/bar.txt');
The phpglob method returns an array of path names matching the provided pattern.
$paths = $this->fileSystem->glob('/foo/*.txt');
The phpget method returns the contents of a file.
$contents = $this->fileSystem->get('/foo/bar.txt');
The phpput 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->put('/foo/bar.txt', 'hello, world!');
The phpprepend method will prepend 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->prepend('/foo/bar.txt', 'hello, world!');
The phpappendContents method will append 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->append('/foo/bar.txt', 'hello, world!');
The phptruncate method will truncate the contents of the file. There's an optional second parameter that will set an exclusive write lock if set to TRUE.
$this->fileSystem->truncate('/foo/bar.txt');
The phpinclude method will include a file.
$this->fileSystem->include('/foo/bar.txt');
The phpincludeOnce method will include a file if it hasn't already been included.
$this->fileSystem->includeOnce('/foo/bar.txt');
The phprequire method will require a file.
$this->fileSystem->require('/foo/bar.txt');
The phprequireOnce method will require a file if it hasn't already been required.
$this->fileSystem->requireOnce('/foo/bar.txt');
The phphash method generates a hash value using the contents of the given file. The default hashing algorithm is sha256 but you can override it using the optional second parameter.
$hash = $this->fileSystem->hash('/foo/bar.txt');
The phphmac method a keyed hash value using the HMAC method using the contents of the given file. The default hashing algorithm is sha256 but you can override it using the optional third parameter.
$hash = $this->fileSystem->hmac('/foo/bar.txt', $secret);
The phpfile method will return a SplFileObject.
$file = $this->fileSystem->file('/foo/bar.txt', 'r');