Index

MMO\core\files

MMO\core\files · 33 symbols · generated 5 Oct 2026

function

copy

files.php:497

Copy a file or folder (recursively), returning the number of files actually copied.

function copy(string $from, string $to, ?string $inc = null, ?string $exc = null): int
Namespace
MMO\core\files
To inspect each individual file as it's copied, use [`loopCopy()`](https://mcp.mmoframework.fr/documentation/mmo.core/php/files.md#loopcopy) directly instead.
$fromstringthe source file/folder
$tostringthe destination
$inc = nullnull | stringa regular expression to select the files to copy
$exc = nullnull | stringa regular expression to exclude the files from being copied
returnsintthe number of files copied
copy('/src/folder', '/dst/folder'); // copies everything, returns the file count
Call graph
function

createDir

files.php:112

Create a folder (recursively) if it does not exist.

function createDir(string $dir, ?int $perms = null): bool
Namespace
MMO\core\files
$dirstringthe folder path
$perms = nullnull | intthe permission to give to the folder. if null, will use its parent permission.
returnsbooltrue if the folder is created, false if the folder already exists.
Call graph
function

csv

files.php:758

Read and decode a CSV file (a UTF-8 BOM, if present, is stripped first). csvDecode().

function csv( $resource, array|bool $keys = true, string $separator = ';', string $enclosure = '"', string $escape = '\\', ?string $lineSep = MMO\core\files\PHP_EOL ): array
Namespace
MMO\core\files
WARNING: a trailing newline at the end of the file content (common -- most text files end with one) produces a spurious extra all-null trailing row; [`csvDecode()`](https://mcp.mmoframework.fr/documentation/mmo.core/php/strings.md#csvdecode) for the underlying cause.
$resourcethe filename, or an open handle on the file
$keys = truearray | boolif true, the first row is used as column names for every other row; if an array, used directly as column names (every row, including the first, is then data)
$separator = ';'stringthe field delimiter
$enclosure = '"'stringthe field quoting character
$escape = '\\'stringthe escape character
$lineSep = MMO\core\files\PHP_EOLnull | stringthe line separator
returnsarray
csv('/tmp/data.csv'); // [['col1' => 'a', 'col2' => 'b']] for a file containing "col1;col2\na;b" (no trailing newline)
Call graph
function

dataURI

files.php:686

Encode a file as a data: URI (its content, base64-encoded, with its detected mime type), ready to use directly as e.g. an <img src="...">.

function dataURI(string $filename): string
Namespace
MMO\core\files
$filenamestring
returnsstring
dataURI('/tmp/tiny.txt'); // e.g. 'data: text/plain;base64,aGk='
Call graph

Uses (1)

function

dataURIToFile

files.php:716

Reverse of dataURI(): decode a data: URI and write its content to $filename.

function dataURIToFile(string $filename, string $data): string
Namespace
MMO\core\files
$filenamestring
$datastringa full data:mime/type;base64,... URI
returnsstringthe mime type extracted from $data
throwsif $data isn't a base64 data URI
dataURIToFile('/tmp/out.txt', 'data: text/plain;base64,aGk='); // writes 'hi', returns 'text/plain'
Call graph

Uses (1)

function

delete

files.php:544

Recursively delete a file or folder. To inspect each individual path as it's deleted, use loopDelete() directly.

function delete(string $path, ?string $inc = null, ?string $exc = null): void
Namespace
MMO\core\files
$pathstringthe file/folder to delete
$inc = nullnull | stringa regular expression to select which entries to delete
$exc = nullnull | stringa regular expression to exclude entries from being deleted
returnsvoid
delete('/some/dir');
Call graph
function

exists

files.php:578

Test if a local file, or a remote url (via a curl HEAD request), exists/is reachable.

function exists(string $url): bool
Namespace
MMO\core\files
$urlstring
returnsbool
exists('/etc/hostname'); // true (for a local file); a remote url is checked via a curl HEAD request
Call graph
function

filename

files.php:266

Get the underlying filename (for a stream) or command (for a process) of a resource.

function filename($var)
Namespace
MMO\core\files
$vara stream or process resource
returnsfalse if $var isn't a stream/process resource, or has no known uri/command
filename(fopen('/tmp/x', 'w')); // '/tmp/x'
Call graph

Used by (1)

function

get

files.php:357

Read the content of a file (given as a filename, read directly) or an already-open stream resource.

function get($stream, ?int $maxlen = null, int $offset = 0): string
Namespace
MMO\core\files
$streama filename, or an open stream resource
$maxlen = nullnull | intmaximum number of bytes to read. null for everything.
$offset = 0intthe offset to start reading from
returnsstring
throwsif $stream isn't readable
get('/etc/hostname');
Call graph
function

image

files.php:697

Load an image file (jpeg/png/gif) as a GD image resource/object.

function image(string $filename, ?array &$size = null)
Namespace
MMO\core\files
$filenamestring
$size = nullnull | array
returns
throwsif the image's mime type isn't jpeg/png/gif
function

isLocal

files.php:563

Tells if a uri refers to the local filesystem: no authority at all (a plain path), or a file:///phar:// scheme. Determines whether exists()/mtime() use plain filesystem calls or a curl HEAD request.

function isLocal(string $url): bool
Namespace
MMO\core\files
$urlstring
returnsbool
isLocal('/local/path'); // true isLocal('file:///local/path'); // true isLocal('https://example.com'); // false
Call graph
function

isMoreRecent

files.php:640

Tells if a file was modified after $time -- or, for a folder (or an already-obtained loopFolder() generator), if ANY file under it was.

function isMoreRecent( Generator|string $filename, int $time, ?string $inc = null, ?string $exc = null ): bool
Namespace
MMO\core\files
$filenameGenerator | stringa filename, or a generator of files (e.g. from loopFolder())
$timeinta unix timestamp to compare against
$inc = nullnull | stringa regular expression of files to include (only used when $filename is a folder)
$exc = nullnull | stringa regular expression of files to exclude (only used when $filename is a folder)
returnsboolfalse if $filename (a single file) doesn't exist
isMoreRecent('/some/dir', time() - 3600); // true if anything in the folder changed in the last hour
Call graph
function

json

files.php:738

Read and decode a JSON file (jsonDecode() for $asArray/$vars/$cleanUp).

function json( $resource, bool $asArray = true, ?array $vars = null, bool $cleanUp = true ): mixed
Namespace
MMO\core\files
$resourcethe filename, or an open handle on the file
$asArray = trueboolif true, a JSON object decodes to a plain PHP array instead of stdClass
$vars = nullnull | arrayvariables to substitute into the JSON before decoding it (a light templating step)
$cleanUp = trueboolif true, tolerate trailing commas and line/block comments (non-standard JSON, not natively supported by PHP)
returnsmixed
throwsif the file's content isn't valid (post-cleanup) JSON
json('/tmp/config.json');
Call graph
function

loopCopy

files.php:467

Recursively copy a file or folder, yielding each individual file as it's copied (a folder itself isn't yielded, only the files under it -- but it IS created, even if empty).

function loopCopy(string $from, string $to, ?string $inc = null, ?string $exc = null)
Namespace
MMO\core\files
$fromstringthe source file/folder
$tostringthe destination
$inc = nullnull | stringa regular expression to select the files to copy (loopFolder())
$exc = nullnull | stringa regular expression to exclude files from being copied (loopFolder())
returnsstring> yields sourceFile => destFile
throwsif $from doesn't exist
throwsif a copy fails
foreach (loopCopy('/src', '/dst') as $from => $to) {...}
Call graph
function

loopDelete

files.php:516

Recursively delete a file or folder (depth-first: a folder's contents are deleted before the folder itself), yielding the path of each thing actually removed as it's removed.

function loopDelete(string $path, ?string $inc = null, ?string $exc = null): Generator
Namespace
MMO\core\files
$pathstringthe file/folder to delete
$inc = nullnull | stringa regular expression to select which entries to delete (loopFolder())
$exc = nullnull | stringa regular expression to exclude entries from being deleted (loopFolder())
returnsGeneratoryields each deleted path
throwsif a delete fails
foreach (loopDelete('/some/dir') as $deleted) {...}
Call graph
function

loopFolder

files.php:160

Lazily list the entries of a folder (files AND subfolders, each yielded individually -- a subfolder is yielded itself, in addition to whatever's recursed into it).

function loopFolder( string $folder, int|bool $recursive = true, ?string $inc = null, ?string $exc = null, string $_path = '' ): Generator
Namespace
MMO\core\files
$folder's last segment can be a glob (a `*`/`**` pattern, e.g. `/some/dir.jpg`) instead of a real folder name: it's resolved automatically by turning it into an $inc regex and listing the parent folder instead -- a single `*.jpg`-style glob also forces $recursive to false, a `**.jpg`-style one forces it to true.
$folderstringthe folder's filename, optionally ending in a glob pattern
$recursive = trueint | boolif true, recurse into every subfolder; if an int, the remaining recursion depth (0 = don't recurse further)
$inc = nullnull | stringa regex the entry's path (relative to $folder) must match to be yielded (null = include everything)
$exc = nullnull | stringa regex that excludes a matching entry (and, if it's a folder, everything under it) entirely
$_path = ''stringinternal: the current path prefix during recursion. Leave at the default.
returnsGeneratorstring> yields relative path => absolute path
throwsif $inc/$exc isn't a valid regex
throwsif $folder doesn't exist (and isn't a resolvable glob)
foreach (loopFolder('/some/dir') as $rel => $abs) {...} // every entry, recursively foreach (loopFolder('/some/dir.jpg') as $rel => $abs) {...} // only *.jpg directly in dir foreach (loopFolder('/some/dir*.jpg') as $rel => $abs) {...} // *.jpg anywhere under dir, recursively
Call graph
function

move

files.php:664

Move file to new destination

function move(string $from, string $to, bool $overwrite = false): void
Namespace
MMO\core\files
$fromstring
$tostring
$overwrite = falseboolif false (default), an existing $to throws instead of being overwritten
returnsvoid
throwsif $from doesn't exist
throwsif $to already exists and either $overwrite is false or $to isn't writable
throwsif the move itself fails
move('/tmp/x.tmp', '/data/x.txt');
Call graph

Uses (1)

function

mtime

files.php:603

Get a file/url's last-modified time, locally via \filemtime() or remotely via a curl HEAD request.

function mtime(string $url): ?int
Namespace
MMO\core\files
$urlstring
returnsnull | inta unix timestamp, or null if $url doesn't exist / isn't reachable / doesn't report a filetime
mtime('/etc/hostname'); // e.g. 1704067200
Call graph

Uses (2)

function

name

files.php:239

Get a short, human-readable identifier for a stream or process resource (for logging/debugging).

function name($var)
Namespace
MMO\core\files
$vara stream or process resource
returns[type[:mode-or-pid]]uri-or-command, or [unknown]unknown ressource for anything else
name(fopen('/tmp/x', 'w')); // '[STDIO:w]/tmp/x'
Call graph
function

open

files.php:316

Open a file and return the resulting stream resource, translating the STREAM_* flag bitmask into the right \fopen() mode (always append-mode when writing, so an existing file's content is preserved unless STREAM_CLEAR is also set -- write() which does the truncation).

function open(string $filename, int $flags = MMO\core\files\STREAM_READ)
Namespace
MMO\core\files
$filenamestring
$flags = MMO\core\files\STREAM_READinta bitmask of STREAM_READ/STREAM_WRITE/STREAM_CLEAR/STREAM_CREATE_FOLDER/STREAM_BINARY
returns
throwsif the file cannot be opened (e.g. the target isn't writable)
open('/tmp/x.log', STREAM_WRITE | STREAM_CREATE_FOLDER);
Call graph
function

package

files.php:821

Package a list of files into a phar package WARNING: when creating a PharData (no stub), the filename MUST NOT have .phar in the extension. Else php throws an Exception.

function package( string $filename, array $files, array|string|null $stub, ?string $alias = null, int $compression = Phar::NONE ): void
Namespace
MMO\core\files
$filenamestringthe phar archive's filename
$filesarraythe files to add as localname => content content can be : - a filename - a directory. In this case localname MUST be the relative path contained in the directory to use in the phar: example: src/files => /home/mmo/src/files - a direct string if it starts with =, - a resource (fopen()), - a Traversable (DirectoryIterator)
$stubarray | string | nullthe stub to use depending on the PHP_SAPI constant if it is a simple string, it is used for all. else it has to be an array[regex => name] where regex is used to test PHP_SAPI (* for any) name is the local filename of the file in the phar archive
$alias = nullnull | stringthe local phar alias to use to call flux operation on the archive. by default the basename of the archive.
$compression = Phar::NONEinta \Phar::* compression constant (e.g. \Phar::GZ, \Phar::BZ2)
returnsvoid
throwsif phar.readonly is enabled in php.ini, or a file/folder to remove/add fails
package('/build/app.phar', ['src' => '/project/src'], 'src/bootstrap.php');
Call graph

Uses (1)

Used by (1)

function

perms

files.php:59

Get a file/folder's permissions formatted like unix ls -l (e.g. 'drwxr-xr-x').

function perms(string $file): ?string
Namespace
MMO\core\files
$filestringfilename
returnsnull | stringnull if $file doesn't exist
perms('/etc/passwd'); // e.g. '-rw-r--r--'
Call graph
function

permsText

files.php:70

Format a raw \fileperms()-style integer as a unix ls -l permission string.

function permsText(int $perms): string
Namespace
MMO\core\files
$permsint
returnsstring
permsText(0100644); // '-rw-r--r--'
Call graph

Used by (2)

function

put

files.php:432

Same as write(), but always appends and creates any missing parent folder (STREAM_PUT = write + create folder).

function put($stream, string $text)
Namespace
MMO\core\files
$stream
$textstring
returns
put('/tmp/new/subdir/x.log', "line\n"); // creates /tmp/new/subdir/ if needed
Call graph
function

rootFolder

files.php:211

Get the deepest common ancestor folder shared by a list of filenames.

function rootFolder(array $files): string
Namespace
MMO\core\files
$filesarraythe files (at least one; absolute paths)
returnsstringends with a trailing '/'. '/' itself if the files share no common folder beyond the filesystem root.
rootFolder(['/a/b/c.txt', '/a/b/d.txt', '/a/e.txt']); // '/a/'
function

scanFolder

files.php:199

Same as loopFolder(), but eagerly collects the results into an array, naturally case-insensitively sorted and then REVERSED (so entries come back in descending natural order).

function scanFolder( string $folder, int|bool $recursive = true, ?string $inc = null, ?string $exc = null ): array
Namespace
MMO\core\files
$folderstring
$recursive = trueint | bool
$inc = nullnull | string
$exc = nullnull | string
returnsarrayrelative path => absolute path, descending natural order
Call graph
function

set

files.php:421

Same as write(), but always replaces the stream's entire existing content (STREAM_SET = write + clear).

function set($stream, string $text)
Namespace
MMO\core\files
$stream
$textstring
returns
set('/tmp/x.log', 'fresh content'); // overwrites whatever was there
Call graph
function

size

files.php:445

Format a byte count as a human-readable size, with French unit abbreviations (o/ko/Mo/Go/To).

function size(string|int $bytes, int $decimals = 2): string
Namespace
MMO\core\files
$bytesstring | intan int byte count, or a filename to \filesize() first
$decimals = 2intnumber of decimals
returnsstring
size(1536); // '1.50 ko' size(1048576); // '1.00 Mo'
function

sort

files.php:135

Sort a list of filenames "naturally" (so 'file2' sorts before 'file10' -- strnatcmp()).

function sort(&$files): array
Namespace
MMO\core\files
$files
returnsarraythe same array, sorted (also returned for convenience/chaining)
sort($files); // ['file2.txt', 'file10.txt'] stays in that order, not lexicographic
function

temporaryFilename

files.php:287

Create an empty, uniquely-named temporary file (in the {tmp} path alias) and return its filename.

function temporaryFilename(string $prefix = '')
Namespace
MMO\core\files
$prefix = ''stringa prefix to use for the filename
returnsthe filename
throwsif the file could not be created
Call graph
function

temporaryFolder

files.php:299

Create an empty, uniquely-named temporary FOLDER (in the {tmp} path alias) and return its filename.

function temporaryFolder(string $prefix = '')
Namespace
MMO\core\files
$prefix = ''stringa prefix to use for the folder's name
returnsthe folder's filename
throwsif the folder was not created
Call graph
function

write

files.php:383

Write $text to a stream.

function write($stream, string $text, int $flags = MMO\core\files\STREAM_WRITE)
Namespace
MMO\core\files
- if $stream is a filename, the file is opened ([`open()`](https://mcp.mmoframework.fr/documentation/mmo.core/php/files.md#open), honoring $flags), written, and closed -- this function then returns null. - if $stream is already an open resource, $text is appended to wherever the stream's cursor currently is (or the stream is truncated back to empty first if $flags includes STREAM_CLEAR); the stream is left open and IS returned, so calls can be chained.
$stream
$textstring
$flags = MMO\core\files\STREAM_WRITEinta bitmask of STREAM_* const, only relevant when $stream is a filename (open())
returns
throwsif $stream is neither a string nor a resource
throwson a write error
write('/tmp/x.log', "line\n", STREAM_PUT); // appends, creating the folder/file if needed
Call graph
function

zip

files.php:773

Open a zip archive, throwing a descriptive exception (rather than returning an error code) on failure.

function zip(string $filename, int $flags): ZipArchive
Namespace
MMO\core\files
$filenamestring
$flagsinta bitmask of \ZipArchive::* open flags (e.g. \ZipArchive::CREATE)
returnsZipArchive
throwsif the archive can't be opened
zip('/tmp/archive.zip', \ZipArchive::CREATE);