PHP fwrite() – writing to a file



PHP fwrite() function is used for writing contents to a file. It write to the file stream pointed to by handle which is created by fopen() function.

Syntax of the PHP fwrite function :
fwrite( file handler, string to write , length )

Length argument is optional , and if we specify a length parameter write will stop when it write will reach to that length bytes.

PHP write() statement is binary-safe , means both binary and character data can be written to a file.

Here is a sample PHP program for writing to a file using fwrite() function

<?php
$file = fopen("syam.txt","w");
fwrite($file,"Here You can learn about how to write to a file");
fclose($file);
?>

This program will create syam.txt if not exist using fopen statement and write the string “Here You can learn about how to write to a file” into that file using the handler $file and fwrite() statement.

Its only a basic tutorial about writing to files. We will discuss more about this in our later posts.