The fwrite Function
The fwrite
function writes data to a file opened with fopen
. The function takes a file pointer as its first parameter, a string to write as the second, and an optional maximum number of bytes to write as the third.
Syntax
fwrite(resource, string, [length]);
Example
Let's write a string to a file:
<?php
$file = fopen('test.txt', 'w');
fwrite($file, 'text');
fclose($file);
?>
Example
Let's write only the first 2
characters:
<?php
$file = fopen('test.txt', 'w');
fwrite($file, 'text', 2);
fclose($file);
?>
Example
Let's append text to the end of the file:
<?php
$file = fopen('test.txt', 'a');
fwrite($file, '!');
fclose($file);
?>
See Also
-
the
fread
function,
which reads from a file -
the
file_put_contents
function,
which writes data to a file