Files & I/O

You will explain following function realeted to files:

  • Opening a file
  • Reading a file
  • Writting a file
  • Closing a file

Syntax:

fopen(filename, mode, include_path, context)

Parameter

filename-Required specifies the file or url to open
mode-Required specifies the type of access you require to the file/stream.

Possible Values of mode

  • "r"(read only)
  • "r+"(read/write)
  • "w"(write only)
  • "w+"(read/write)
  • "a"(Opens the file for writing only.
    Places the file pointer at the end of the file.)
  • "a+"(read/write)
  • "x"(write only)
  • "x+"(read/write)

Example
Mode: Reading a text file

<?php
$f=fopen("data.txt","r")or die ("unable to open file");
$r=fread($f, filesize("data.txt"));
echo $r;
fclose($f);
?>

Output is:
Here is the text file display.
Example
Mode: Writting a file

<?php
$create =fopen("news.txt","w");
fwrite($create,"How are you");
fclose($create);
?>