pTutorial

New way to learn PHP

PHP 5 File append mode

Append mode

Append mode is a kind of file mode where you can write the data of exiting file with exiting content that means there will be no loss of any existing data that are already present in the file. If file is not there PHP attempt to create in this mode.


There are two type of append mode available in PHP.

  1. a
  2. a+

a mode

a is used to only appending the data at the end of the file if file is not there PHP attempt to create it.

Example of append data on file

<?php

$fh = fopen("ptutorial.txt","a") ;

$u="Mr brown";

fwrite($fh, $u);

$u="Mr victor";

fwrite($fh, $u);

fclose($fh);

?>

Data inside the txt file

Mr. John Mr brownMr victor

In above case you can only append the text into the file, you cannot read the data from file.

a+ mode

a+ is used to append and read the data or text of the file, writing will be at the end of the file.

Example of append file data

<?php

$fh = fopen("ptutorial.txt","a+") ;

$u="Mr brown";

fwrite($fh, $u);

$u="Mr victor";

fwrite($fh, $u);

fclose($fh);

?>

Example of read the data on append mode

<?php

$fh = fopen("pt.txt","a+");

$s = fread($fh,5000000);

print $s;

fclose($fh);

?>

Mr. John Mr brownMr victorMr brownMr victor

You can read as well as write the data or text in case of a+ mode as shown in above example.


Note:Writing start from the end of the file in both a and a+ mode.


QUOTES OF THE DAY

A good beginning makes a good ending.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER