This section contains the detail about writing file in PHP.
In PHP, fwrite() function is used to write a text file. The fwrite() function allows to write to any type of file.
The fwrite() function takes two parameters : first one is the file handle . This passed file handle must of the file which you are going to write. The second parameter is the string to be written. And an optional parameter is also available it is length. If the length argument is given, writing will stop after length bytes have been written or the end of string is reached, which ever comes first.
Given below the syntax :
int fwrite ( resource $handle , string $string [, int $length ] )
The fwrite() returns the number of bytes written, or FALSE on error.
Given below the example :
<?php // PHP code to write to a file $myFileName = 'C:\testFile.txt'; $myFileHandle = fopen($myFileName, 'w') or die("cant open file"); $str="Devmanuals\n"; fwrite($myFileHandle, $str); $str = "PHP Tutorial\n"; fwrite($myFileHandle, $str); fclose($myFileHandle); ?>
The testFile.txt contains following after write :