fwrite
fwrite
Binary-safe file write
**fwrite** resource $stream string $data $length
writes the contents of
to the file stream pointed to by
.
fwrite``data``stream
stream``dataThe string that is to be written.
length
If is an integer, writing will stop
after bytes have been written or the
end of is reached, whichever comes first.
length``length``data
returns the number of bytes
written, return.falseforfailure.
fwrite
raises on failure.
fwrite``E_WARNING
Voorbeeld: A simple example
<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$fp = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($fp, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($fp);
} else {
echo "The file $filename is not writable";
}
?>
Opmerking: > Writing to a network stream may end before the whole string is written. Return value of may be checked:
`fwrite````php
Opmerking: > On systems which differentiate between binary and text files (i.e. Windows) the file must be opened with 'b' included in mode parameter.
fopen
Opmerking: > If was ed in append mode, s are atomic (unless the size of exceeds the filesystem's block size, on some platforms, and as long as the file is on a local filesystem). That is, there is no need to a resource before calling ; all of the data will be written without interruption.
stream``fopen``fwrite``data``flock``fwrite
Opmerking: > If writing twice to the file pointer, then the data will be appended to the end of the file content:
<?php $fp = fopen('data.txt', 'w'); fwrite($fp, '1'); fwrite($fp, '23'); fclose($fp); // the content of 'data.txt' is now 123 and not 23! ?>
fread``fopen``fsockopen``popen``file_get_contents``pack