PHP fwrite() Function
In this chapter you will learn:
- Definition for PHP fwrite() Function
- Syntax for PHP fwrite() Function
- Parameter for PHP fwrite() Function
- Return for PHP fwrite() Function
- Note on PHP fwrite() Function
- Example - Write to a file
Definition
The fwrite() writes to an open file.
Syntax
PHP fwrite() Function has the following syntax.
fwrite(file,string,length)
Parameter
Parameter | Is Required | Description |
---|---|---|
file | Required. | Open file to write to |
string | Required. | String to write to the open file |
length | Optional. | Maximum number of bytes to write |
Return
This function returns the number of bytes written, or FALSE on failure.
Note
file_put_contents() and fwrite() complement functions file_get_contents() and fread(), respectively.
fwrite() works with the file handle returned by fopen().
fwrite() takes a string and an optional length to write as the third parameter.
If you do not specify the third parameter, all of the second parameter is written out to the file.
Example
Write to a file
<?PHP/* jav a2 s .c om*/
$filename = "test.txt";
$handle = fopen($filename, "wb");
$numbytes = fwrite($handle, "java2s.com");
fclose($handle);
print "$numbytes bytes written\n";
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP getcwd() function
- Syntax for PHP getcwd() function
- Return for PHP getcwd() function
- Example - Get current working directory
Home » PHP Tutorial » PHP File Functions