PHP File Write

The fwrite() function is a main function that is used to write something into files.

This function returns the number of bytes written, or FALSE on failure.



$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


Before we can write information to our test file we have to use the function fopen to open the file for writing.

$myFile = “testfile.txt”;
$fh = fopen($myFile, ‘w’) or die(“can’t open file”);
$stringData = “First comment – write into text file”;

The $fh variable contains the file handle for testfile.txt. The file handle knows the current file pointer, which for writing, starts out at the beginning of the file.

Now open the testfile.txt.

Posted in Php