fwrite()
is a built-in function in PHP.
The fwrite()
function writes data to an open file. The data is passed a string.
The function will continue to write to the file until we either reach the end of the file, or attain the specified length, whichever comes first.
The following is the function prototype:
fwrite(file, string, length)
The fwrite()
function takes the following input parameters:
file
: The file to which we write.
string
: The data, as a string, to be written to the file.
length
: An optional parameter that specifies the maximum number of bytes to be written.
The function returns the total number of bytes written in case of successful execution. Otherwise, the function returns FALSE
.
<?php$file = fopen("Edpresso.txt","r+");echo fwrite($file,"Edpresso: Concise shots of dev knowledge!");fclose($file);echo "\n";$file = fopen("Edpresso.txt","r+");echo fgets($file);fclose($file);?>
In the example above, we open the Edpresso.txtfile. The data is written to the file using the
fwrite()` function. The integer output represents the total number of bytes written to the file.
Next, to verify that the data has been successfully written to the file, we use fgets()
to read data from the file.
Free Resources