Skip to main content
Version: Fegg 5.x (next)

File::writeFile

Write data to a file

Description​

public void File::writeFile( string $file, string $data, [ string $writeOption = 'a' ] )

Opens $file with the fopen() mode $writeOption, writes $data and sets the permission of the file to 0666. The default mode a appends to the file; use w to overwrite it.

Arguments​

NameData typeDefaultRemarks
$filestringFile to write
$datastringData to write
$writeOptionstring'a'fopen() mode (a: append, w: overwrite)

Example​

Controller
use Fegg\Tool\File;

$file = new File();

// Append a line
$file->writeFile('path/to/foo.log', date('Y-m-d H:i:s') . " done\n");

// Overwrite the file
$file->writeFile('path/to/foo.txt', 'New contents', 'w');