MilkCore

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
example

// Write data safely if (File::put_contents('data.txt', 'Hello World')) { echo "File written successfully"; } else { echo "Error: " . File::get_last_error(); }

// Read data safely $content = File::get_contents('data.txt'); if ($content !== false) { echo "Content: " . $content; } else { echo "Error: " . File::get_last_error(); }

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
example

// Logging to a file $log_entry = date('Y-m-d H:i:s') . " - User logged in\n"; if (File::append_contents('app.log', $log_entry)) { echo "Log entry added successfully"; } else { echo "Failed to write log: " . File::get_last_error(); }

example

// Adding items to a list file $new_item = "Item #" . rand(1, 1000) . "\n"; if (File::append_contents('items.txt', $new_item)) { echo "Item added to list"; }

example

// CSV data appending $csv_row = "John,Doe,30,Engineer\n"; if (File::append_contents('users.csv', $csv_row)) { echo "User data appended to CSV"; }

example

// Multiple appends in sequence $messages = [ "Message 1\n", "Message 2\n", "Message 3\n" ]; foreach ($messages as $message) { File::append_contents('messages.txt', $message); }

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
example

$content = File::get_contents('/path/to/file.txt'); if ($content !== false) { echo "File content: " . $content; } else { echo "Read failed: " . File::get_last_error(); }

example

// Reading a JSON configuration file $config_json = File::get_contents('config.json'); if ($config_json !== false) { $config = json_decode($config_json, true); }

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
example

if (!File::put_contents('readonly.txt', 'data')) { $error = File::get_last_error(); error_log("File operation failed: $error"); echo "Operation failed. Check logs for details."; }

example

// Error handling in a loop $files = ['file1.txt', 'file2.txt', 'file3.txt']; foreach ($files as $file) { if (!File::put_contents($file, 'test data')) { echo "Failed to write $file: " . File::get_last_error() . "\n"; } }

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
example

if (File::put_contents('log.txt', "Error occurred at " . date('Y-m-d H:i:s'))) { echo "Log entry written successfully"; } else { echo "Failed to write log: " . File::get_last_error(); }

example

// Writing JSON data $data = ['user' => 'john', 'timestamp' => time()]; $json = json_encode($data); if (File::put_contents('user_data.json', $json)) { echo "User data saved"; }

example

// Appending to a file (manual approach) $existing = File::get_contents('messages.txt'); $new_content = $existing . "\nNew message"; File::put_contents('messages.txt', $new_content);

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
example

// Exclusive lock for writing $fp = fopen('data.txt', 'c'); if (File::wait_lock($fp, true)) { fwrite($fp, 'Exclusive write access'); flock($fp, LOCK_UN); } fclose($fp);

example

// Shared lock for reading $fp = fopen('data.txt', 'r'); if (File::wait_lock($fp, false)) { $content = fread($fp, filesize('data.txt')); flock($fp, LOCK_UN); } fclose($fp);

see
flock()

For more information about file locking

see
LOCK_EX

For exclusive locks

see
LOCK_SH

For shared locks

Return values
bool

True if lock was successfully acquired, false on timeout


        
On this page

Search results