Share on Google+Share on Google+

The PHP file_put_contents() function , file_put_contents php

Advertisement
The PHP file_put_contents() function is used to write the string into a file. It is identical to using the fopen(), fwrite() and fclose() functions.

Learn how to use file_put_contents() function in PHP

Example of the PHP file_put_contents() function

Description

Syntax of file_put_contents() PHP

file_put_contents(file_name,data_to_file,[file_mode,[file_context]])

It writes the data to the file .It overwrites the previous contents.

file_mode mode for the file writing with the constant values

  • FILE_USE_INCLUDE_PATH
  • FILE_APPEND
  • LOCK_EX

Code & Example of file_put_contents() PHP

<?php

$string1=file_get_contents("c:/rose1/ram.txt");

echo $string1;

$data1="\nindia is my country";

file_put_contents("c:/rose1/ram.txt",$data1);

$string2=file_get_contents("c:/rose1/ram.txt");

echo "<br>".$string2;

?>

Output

1.

welcome to rosindia welcome to rosindia welcome to rosindia
india is my country

2. 

india is my country
india is my country

 

Advertisements

Posted on: September 24, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.