Oihana PHP

makeFile.php

Table of Contents

Functions

makeFile()  : string
Creates or updates a file with the given content and options.

Functions

makeFile()

Creates or updates a file with the given content and options.

makeFile(array{append?: bool, content?: string|null, file?: string|null, force?: bool, group?: null|string, lock?: bool, overwrite?: bool, permissions?: int, owner?: string|null}|string|null $fileOrOptions[, string|null $content = null ][, array{append?: bool, force?: bool, group?: null|string, lock?: bool, overwrite?: bool, permissions?: int, owner?: string|null} $options = [] ]) : string

This function writes content to the specified file path. It supports appending to existing files, overwriting, setting file permissions, changing ownership, and group. It can also create the parent directories if needed.

Usage:

  • Classic signature:
makeFile(string $filePath, string $content = '', array $options = []);
  • Signature with options array as first parameter:
makeFile(array $options);

Required keys in $options:

  • 'filePath' (string): The path of the file to create or modify (mandatory).
  • 'content' (string): The content to write into the file (optional, default ''). Other keys correspond to options (see below).
Parameters
$fileOrOptions : array{append?: bool, content?: string|null, file?: string|null, force?: bool, group?: null|string, lock?: bool, overwrite?: bool, permissions?: int, owner?: string|null}|string|null

Either the file path as a string (classic usage), or an associative array containing at least 'filePath' and optionally 'content' and other options.

$content : string|null = null

The content to write into the file. Defaults to empty string. Ignored if $filePathOrOptions is array.

$options : array{append?: bool, force?: bool, group?: null|string, lock?: bool, overwrite?: bool, permissions?: int, owner?: string|null} = []

An associative array of options:

  • 'append' (bool): If true, appends content instead of overwriting. Default: false.
  • 'force' (bool): If true, creates parent directories if they do not exist. Default: true.
  • 'group' (string|null): Group name or ID to set as file group owner. Default: null.
  • 'lock' (bool): If true, uses an exclusive lock while writing. Default: true.
  • 'overwrite' (bool): If true, overwrites existing files. Default: false.
  • 'permissions' (int): File permissions to set (octal). Default: 0644.
  • 'owner' (string|null): User name or ID to set as file owner. Default: null.
Tags
throws
FileException

If the file path is invalid, writing fails, or permission/ownership changes fail.

throws
DirectoryException

If directory creation fails.

author

Marc Alcaraz (ekameleon)

author

Marc Alcaraz (ekameleon)

since
1.0.0
since
1.0.0
example

Create a new file with content, creating directories if needed

makeFile('/path/to/file.txt', "Hello World");

Append content to an existing file, creating directories if needed

makeFile('/path/to/file.txt', "\nAppended line", ['append' => true]);

Overwrite existing file with new content

makeFile('/path/to/file.txt', "Overwrite content", ['overwrite' => true]);

Create a file with custom permissions and without locking

makeFile('/path/to/file.txt', "Content", ['permissions' => 0600, 'lock' => false]);

Create a file and set ownership and group (requires appropriate permissions)

makeFile('/path/to/file.txt', "Content", ['owner' => 'username', 'group' => 'groupname']);

Create a file without forcing directory creation (will fail if directory missing)

makeFile('/path/to/file.txt', "Content", ['force' => false]);

With a unique array definition :

 makeFile([
     'file'        => '/path/to/file.txt',
     'content'     => "Hello World",
     'append'      => true,
     'permissions' => 0600,
 ]);
Return values
string

The path of the created or updated file.


        
On this page

Search results