PHP supports file handling which is used to read, write and append data to the file.



This chapter will demonstrate to you following PHP functions related to files:

File Opening in PHP

fopen() function is used to open a file in PHP. Its required two arguments, first the file name and then file opening mode.

The file may be opened in one of the following modes:
r Read-only. Starts at the beginning of the file
r+ Read/Write. Starts at the beginning of the file
w Write only. Opens and clears the contents of the file; or creates a new file if it doesn't exist
w+ Read/Write. Opens and clears the contents of the file; or creates a new file if it doesn't exist
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 the file already exists
x+ Read/Write. Creates a new file. Returns FALSE and an error if the file already exists

Example:

<?php
$fileName = "/doc/myFile.txt";
$fp = fopen($fileName,"r");
if( $fp == false )
{
  echo ( "Error in opening file" );
  exit();
}
?>

If an attempt to open a file fails then fopen returns a false value, otherwise it returns a file pointer which is used for further reading or writing of that file.

File Reading in PHP

Once a file is opened using fopen() function then it can be read by a function called fread(). This function requires two arguments. The file pointer and length in bytes of the file must be expressed.

The files size can be calculated using the filesize() function which takes the file name as its argument and returns the size of the file in bytes.

Example:

<?php
$fileName = "/doc/myFile.txt";
$fp = fopen($fileName,"r");
if( $fp == false )
{
  echo ( "Error in opening file" );
  exit();
}

$fileSize = filesize( $fileName );
$fileData = fread( $fp, $fileSize );
?>

Reading a File Line by Line

The fgets() function is used to read a single line from a file, and after a call to this function, the file pointer has moved to the next line.

Example:

<?php
$fileName = "/doc/myFile.txt";
$fp = fopen($fileName,"r");
if( $fp == false )
{
  echo ( "Error in opening file" );
  exit();
}
  
while(!feof($fp))
{
  echo fgets($fp). "<br>";
}
?> 

File Writing in PHP

Using the PHP fwrite() function, a new file can be written, or text can be appended to an existing file. This function requires two arguments, first specifying a file pointer and the string of data that is to be written. Optionally a third integer argument can be used to specify the length of the data to write. If the third argument is used, file writing will stop after the specified length has been reached.

Example:

<?php
$fileName = "/doc/myFile.txt";
$fp = fopen($fileName,"w");
if( $fp == false )
{
  echo ( "Error in opening file" );
  exit();
}
fwrite( $fp, "This is a sample text to write\n" );
?>

Closing a File in PHP

In PHP it is not system critical to close all your files after using them because the file will auto close after PHP code finishes execution.

You can close file, using fclose() function.

Example:

<?php
$fileName = "/doc/myFile.txt";
$fp = fopen($fileName,"w");
if( $fp == false )
{
  echo ( "Error in opening file" );
  exit();
}
  
//some code to be executed
  
fclose( $fp );
?>

The fclose() function requires a file pointer as its argument and then returns true when the closure succeeds or false if it fails.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram