Skip to content

PHP Write to File – fwrite() Function

Advertisement

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

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.

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top