PHP fwrite() function used to write data to file. This function take three arguments

Advertisement

1. File specifies filename to write
2. String specifies the string to write to file
3. Lenght specifies maximum number of bytes to write to file (optional)

Syntax

PHP fwrite() function uses following syntax

 fwrite(file, string, length)

Example

Below is the sample example of writing data to file using php script. In below example first line will open myfile.txt in edit mode in current directory. Second line will write string to file, you can also specify the lenght here and result the number of bytes written. In case of failure, FALSE will be returned. Third line will close open file.

<?php
  $fn = fopen("myfile.txt","w");
  $result = fwrite($fn,"Welcome to PHP!");
  fclose($fn);
?>

Output:

This function returns total number of bytes written to a file or returns FALSE in case of failure.

Share.
Leave A Reply

Exit mobile version