PHP fwrite()Functions


PHP Filesystem 参考手册Complete PHP Filesystem Reference Manual

Definition and Usage

The fwrite() function writes content to an open file.

The function stops running when it reaches the specified length or reads the end of file (EOF), whichever comes first.

If the function executes successfully, it returns the number of bytes written. If it fails, it returns FALSE.

Syntax

fwrite(file,string,length)

Parameters Description
file Required. Specifies the open file to write to.
string Required. Specifies the string to write to the open file.
length Optional. Specifies the maximum number of bytes to write.


Tips and Notes

Tip:This function is binary-safe. (Meaning binary data (such as images) and character data can both be written using this function.)


Example

<?php
$file = fopen("test.txt","w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);
?>

The above code will output:

21


PHP Filesystem 参考手册Complete PHP Filesystem Reference Manual Other extensions