Oihana PHP

clearFile.php

Table of Contents

Functions

clearFile()  : bool
Clears the content of a file while keeping the file itself.

Functions

clearFile()

Clears the content of a file while keeping the file itself.

clearFile(string|null $file[, bool $assertable = true ]) : bool

This function empties the given file. Returns true if the file was successfully cleared, false otherwise.

The behavior on failure depends on the $assertable parameter:

  • If $assertable is true (default), a FileException is thrown if the file does not exist or is not writable.
  • If $assertable is false, no exception is thrown; the function simply returns false on failure.
Parameters
$file : string|null

The full path to the file to clear.

$assertable : bool = true

Whether to throw exceptions on failure (default: true).

Tags
throws
FileException

If $assertable is true and the file does not exist or is not writable.

example
use function oihana\files\clearFile;

$file = '/path/to/file.txt';

// Clear the file, throwing exception on failure
$success = clearFile($file);

// Clear the file, returning false instead of throwing an exception
$success = clearFile($file, assertable: false);
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
bool

True if the file was cleared successfully, false otherwise.


        
On this page

Search results