PHP offers many functions for file manipulation. The fputs
function in PHP writes content to an already open file; it is similar to the fwrite
function.
The syntax for the fputs
function is shown below:
fputs(resource file, string str, int length)
The fputs
function can take three parameters:
file
: This is a required parameter. file
is the pointer of the file stream that points to an already open file. The string will be written to this file.str
: This is a required parameter. str
is the string we want to write to the open file.length
: This is an optional parameter. The length
parameter specifies the number of bytes to write to the file.Upon successful execution, the fputs
function returns the number of bytes written to the file. Upon failure or error, it returns false
.
The following code shows how we can use the fputs
function. The function writes the text "Hello World!"
to the file and displays the number of characters written.
<?php$file = fopen("test.txt","w");echo fwrite($file,"Hello, World!");fclose($file);?>
Free Resources