File
in package
Static file handling class with mandatory file locking
This class provides thread-safe file operations using exclusive locks. All operations are atomic and prevent file corruption during concurrent access.
Tags
Table of Contents
Methods
- append_contents() : bool
- Appends data to a file with mandatory exclusive locking
- get_contents() : string|false
- Reads the entire file content with mandatory file locking
- get_last_error() : string
- Retrieves the last error message
- put_contents() : bool
- Writes data to a file with mandatory exclusive locking
- wait_lock() : bool
- Waits to acquire a lock on a file handle
Methods
append_contents()
Appends data to a file with mandatory exclusive locking
public
static append_contents(string $file_path, string $data) : bool
This method appends the provided data to the end of an existing file, or creates a new file if it doesn't exist. It uses exclusive locking to prevent concurrent writes and data corruption during append operations.
Parameters
- $file_path : string
-
The path to the file to append to
- $data : string
-
The data to append to the file
Tags
Return values
bool —True on successful append, false on failure
get_contents()
Reads the entire file content with mandatory file locking
public
static get_contents(string $file_path) : string|false
This method opens the file with a shared lock to ensure data consistency during read operations. It waits up to 10 seconds to acquire the lock.
Parameters
- $file_path : string
-
The path to the file to read
Tags
Return values
string|false —The file content as string on success, false on failure
get_last_error()
Retrieves the last error message
public
static get_last_error() : string
Returns the error message from the most recent file operation that failed. The error message is automatically cleared when a new operation starts.
Tags
Return values
string —The last error message, empty string if no error occurred
put_contents()
Writes data to a file with mandatory exclusive locking
public
static put_contents(string $file_path, string $data) : bool
This method creates or overwrites the file with the provided data. It uses exclusive locking to prevent concurrent writes and data corruption. The file is truncated before writing to ensure clean content.
Parameters
- $file_path : string
-
The path to the file to write
- $data : string
-
The data to write to the file
Tags
Return values
bool —True on successful write, false on failure
wait_lock()
Waits to acquire a lock on a file handle
public
static wait_lock(resource $fp[, bool $exclusive = true ]) : bool
This method attempts to acquire either an exclusive lock (LOCK_EX) for writing or a shared lock (LOCK_SH) for reading on the given file handle using non-blocking calls. It retries up to 200 times with 50ms intervals, providing a total timeout of 10 seconds.
Shared locks allow multiple readers but block writers. Exclusive locks block all other access (readers and writers).
Parameters
- $fp : resource
-
The file handle to lock
- $exclusive : bool = true
-
True for exclusive lock (LOCK_EX), false for shared lock (LOCK_SH)
Tags
Return values
bool —True if lock was successfully acquired, false on timeout