Home >>Advance PHP Tutorial >PHP File create write

PHP File create write

PHP create file and write contents

PHP comes with a couple of different ways to do this as well. To write inside the file first file must be opened with mode.

How to Open a file in PHP

fopen( ) is used to open a file. You have to pass two parameters inside this function. The first one is file name which you want to open and second is mode(purpose to open) of the file. Syntax
<?php

$fo=fopen("filename","mode");

?>

Different types of File MODE

The file may be opened in one of the following modes:
Modes Description
w Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist
w+ Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist
r Read only. Starts at the beginning of the file
r+ Read/Write. Starts at the beginning of the file
a Append. Opens and writes to the end of the file or creates a new file if it doesn't exist
a+ Read/Append. Preserves file content by writing to the end of the file
x Write only. Creates a new file. Returns FALSE and an error if file already exists
x+ Read/Write. Creates a new file. Returns FALSE and an error if file already exists

Write the contents inside file

1 file_put_contents() The first is the file_put_contents()function, a close cousin of the file_get_contents() you will read about later. file_put_contents() accepts a filename and path, together with the data to be written to the file, and then writes the latter to the former Eg
<?php

//write string to file

$data="A fish out of water";

file_put_contents("output.txt",$data) or die('ERROR:Can not write file');

echo "data written inside  this file";

?>

If the file specified in the call to file_put_contents() already exists on disk,.file_put_contents() will overwrite it by default. if instead, you'd prefer to preserve the file's contents and simply append new data to it, and the special FILE_APPEND flag to your .file_put_contents() function call as a third argument. Eg
<?php

//write string to file

$data="A fish out of water";

file_put_contents("output.txt",$data,FILE_APEND)or die('ERROR:Can not write file');

echo "data written inside  this file";

?>


Write the contents using fwrite( ) function

An alternative way to write data to a file is to create a file pointer with fopen( ), and then write data to the pointer using PHP's fwrite( ) function. Eg
<?php

//open and lock file 

//write string to file

//unlock and close file

$data="A fish out of water"; 

$fo=fopen("output.txt","w");

flock($fo,LOCK_EX) or die('ERROR:cannot lock file');

fwrite($fo,$data);

flock($fo,LOCK_UN) or die('ERROR:cannot unlock file');

fclose($fo);

echo "Data written to file";

?>


Write the contents using fputs( ) function

An alternative way to write small data to a file is fputs( ) function. Eg
<?php

//open and lock file 

//write string to file

//unlock and close file

$data="A fish out of water"; 

$fo=fopen("output.txt","w");

flock($fo,LOCK_EX) or die('ERROR:cannot lock file');

fputs($fo,$data);

flock($fo,LOCK_UN) or die('ERROR:cannot unlock file');

fclose($fo);

echo "Data written to file";
 
?>


No Sidebar ads