File Write

File Write


Posted in : PHP Posted on : January 6, 2011 at 4:33 PM Comments : [ 0 ]

This section contains the detail about writing file in PHP.

File Write

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);
?>

Output

The testFile.txt contains following after write :

Devmanuals
PHP Tutorial

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics