How to write a file in PHP

How to write a file in PHP

To Open a new file for writing we can use
fopen($file_name,’w') Or we can use fopen($file_name,’a')
to append our content to existing file.

Three simple steps to write the content of a string to text file

1. Open the file in Write or append mode
$fp = fopen(”/path/save.txt”, “w”);

2. Use fwite command to write the string using above created file pointer
fwrite($fp,$string_to_be_written);

3.Close the file pointer
fclose($fp);


<?php

$string_to_be_written = 'File content to be written';

$fp = fopen("/path/save.txt", "w");
fwrite($fp,$string_to_be_written);
fclose($fp);

?>

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.