An object-oriented way of dealing with files and performing actions on them.
<?php
use MeesterDev\FileWrapper\File;
$file = new File();
$file->cd('..');
$file->select('data.json');
$json = $file->readJson();
All methods from SplFileInfo are available. Further methods are described below.
An instance of File
can point to anything: directories, files, links, and even things that do not exist.
There is no distinction made between files and directories, until actions are performed that require either type.
The current path the file is pointing to.
The default constructor. You can use both a relative and absolute path. Note that a relative path is applied from the current working directory (getcwd()
).
Create a temporary file in the given directory. The name will be prefixed with $prefix
. This relies on the PHP functionality tmpnam()
, which may or may not do exactly follow the given arguments. Regardless, the returned File
's $path
property will be set to the created file.
Scans and returns all directories and files contained in the file. This will only work if the file itself is a directory.
Scans and returns all directories and files contained in the file that match the $search
string. This will only work if the file itself is a directory. $flags
will be passed directly to the native glob()
function.
Returns an array of the lines contained in the file. This only works on files. $flags
and $context
are passed directly the native file()
function.
Prints the file to STDOUT. The $context
is passed directly to the native readfile()
function.
Returns the contents of the file. All arguments are passed directly to file_get_contents()
.
Returns the mimetype of the file.
Parses the file as JSON and returns the parsed content. All arguments are passed to the native json_decode()
function. Note that the JSON_THROW_ON_ERROR
flag is always set.
Returns an array of rows from the CSV file. The file is loaded in its entirety. All arguments are passed to the native fgetcsv()
method.
Parses the file as an INI file and returns the result. The arguments are passed directly to the native parse_ini_file()
function.
$file->readXml(int $options = 0, bool $dataIsUrl = false, string $namespace = '', bool $isPrefix = false): SimpleXMLElement
Returns a SimpleXMLElement constructed from the file's content. All arguments are directly passed to the constructor.
Returns a DOMDocument constructed from the file's content. $xmlFlags
is passed to the DOMDocument
's loadXML()
method. $context
is passed to the $file
's contents
method.
Same as above, but uses the HTML loading methods of DOMDocument.
Writes the lines to the file.
Writes the content to the file. The arguments are passed directly to file_put_contents()
.
Encodes $contents as JSON and writes it to the file. The $depth
and $flags
are passed directly the json_encode()
. Note that the JSON_THROW_ON_ERROR
flag is always set.
$file->writeCsv(mixed[][] $rows, string $mode = File::MODE_WRITE, string $separator = ',', string $enclosure = '"', string $escape = '\'): File
Writes the rows to the file as CSV. $mode
is the file mode used to open the file with for writing. $separator
, $enclosure
, and $escape
are passed to the fputcsv()
method.
Writes the SimpleXMLElement to the file (as XML).
Writes the DOMDocument to the file (as XML). The $rootNode
and $flags
arguments are passed to the saveXML()
method.
Same as above, but uses the HTML saving methods of DOMDocument.
Changes the group of the file. Usually only possible as root.
Changes the permissions of the file. Usually only possible as the owner or as root.
Changes the owner and group (if supplied) of the file. Usually only possible as root.
Copies the file and returns a new instance pointing to the new file. You can set $relativeFromCurrentFile
to false to tell the class to not do any modifications with $destination
, in which case the argument is passed directly to the native copy()
function. The $context
is passed regardless.
Creates a hard link. This only works on files. You can set $relativeFromCurrentFile
to false to tell the class to not do any modifications with $name
, in which case the argument is passed directly to the native link()
function.
Same as above, but creates a symbolic link, thus making it work on other things too (e.g. directories).
Returns the link info of the file. See linkinfo()
.
Returns a new File pointing to the target of a (symbolic) link.
Returns the result of lstat of the file. See lstat()
.
Creates a subdirectory of the file. The current file must be a directory. All arguments are passed to mkdir()
.
Moves the file to $to
, if it is an uploaded file. You can set $relativeFromCurrentFile
to false to tell the class to not do any modifications with $destination
, in which case the argument is passed directly to the native move_uploaded_file()
function. Returns the new file.
Renames the file to $to
. You can set $relativeFromCurrentFile
to false to tell the class to not do any modifications with $destination
, in which case the argument is passed directly to the native rename()
function. $context
is always passed. Returns the new file.
Deletes the file if it is a file, or the directory if it is a directory and $recursive
is true
. The $context
is passed to all calls made to unlink()
. If deletion of any file fails, this is silently ignored until all deletions have been attempted, at which point an exception is thrown. If scanning of a directory fails, the same logic applies.
Returns the result of lstat of the file. See stat()
.
Touches a file, creating it. The arguments are passed to touch()
.
Returns a new File pointing to the new path. The new path is built from the current path and the given path.
Returns a printable relative path for the file. This is always relative to the current working directory. If $force
is set to true, the path will be relative, even if the first folder is different.
On Windows, if the file and working directory are on different drives, an absolute path is returned, even if $force
is set to true.
Returns true if the file name of the File matches the given pattern. The arguments are passed directly to fnmatch()
.
Returns the available disk space, if PHP can determine it.
Returns the total disk space, if PHP can determine it.
Returns $file->path
.