3/31/15

PHP File Create Write


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:
ModesDescription
wWrite 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
rRead only. Starts at the beginning of the file
r+Read/Write. Starts at the beginning of the file
aAppend. 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
xWrite 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 i
<?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 ii
<?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";
?>

2 How to use 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 iii
 <?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";
 ?>

2 How to use fputs( ) function

An alternative way to write small data to a file is fputs( ) function.
Eg iv
 <?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";
 ?>

0 comments:

Post a Comment

FIND US ON FACEBOOK

FIND US ON Twitter