PHP

PHP File Handling fopen fread and fclose Example

PHP has the File handling functionalities that allows us to make dynamic operations on files.

In the previous example we have discussed about how to open a file in Php. Now we will see some of the other file handling functions that include fopen(), fread(), and fclose().

php fopen() :

The fopen() is a function in php, which is used to open a file. This function is better than readfile() to handle the file operations. Let us see how it works.

Sample.txt

Sample.txt
Sample php readfile example
php tutorials
php examples

fopen() Example :

<?php
$file = fopen("/home/chandrashekhar/Desktop/sample.txt", "r") or die("Unble to open the file ");
echo fread($file,  filesize("/home/chandrashekhar/Desktop/sample.txt"));
fclose($file)
?>

Output:

Sample php readfile example php tutorials php examples

The fopen() function takes  two parameters, one is file path and second one is mode (In which mode the file to be open). On the above example we have given as "r", that means readonly. Here is the list of all the possible modes.

  • r   : Read only
  • w  : Write only
  • a   : Append. For adding content to an existing file
  • x   : Create new write file.
  • r+ : Read/write. It may preserve file content.
  • w+ : Read/write. Erases all contents of a file
  • a+  : Read/write. Preserves content but adds more.
  • x+  : New file. Returns FALSE if file exists.

php fread() :

It reads contents of a file that is already open. It is used in the same way as the readfile() function.

fread($myfile,filesize("/home/chandrashekhar/Desktop/sample.txt"));

fclose () closes an open file.

fclose($myfile);

It is responsible to close the file after using to minimize resource usage and for security reasons.

php fgets() :

This function reads single lines from a file.

<?php
$file = fopen("/home/chandrashekhar/Desktop/sample.txt", "r") or die("Unble to open the file ");
echo fgets($file);
fclose($file)
?>

Output:

Sample php readfile example

php feof() :

It checks for the end of a file. It may be useful when running loops.

<?php

$file = fopen("/home/chandrashekhar/Desktop/sample.txt", "r") or die("Unble to open the file ");
while (!feof($file)) {
echo fgets($file) . "</br>";
}
fclose($file)
?>

Output:

Sample php readfile example
php tutorials
php examples

php fgetc() :

It reads single characters from a file.

<?php

$file = fopen("/home/chandrashekhar/Desktop/sample.txt", "r") or die("Unble to open the file ");
while (!feof($file)) {
echo fgetc($file)."</br>";
}
fclose($file)
?>

After reading all the characters, the curser moves after the read character.

Happy Learning :)

Found a mistake or something out of date? Tell us →

Written by

chandrashekhar

Founder of onlinetutorialspoint.com. Enterprise architect with 15+ years in healthcare technology, designing and running systems on AWS and Azure. Writes about building real things with AI, local AI hardware and cloud certifications.

Keep reading

C Programming

File Handling in C

In this tutorial, we are going to learn about reading and writing files in C. File Handling in C: The English dictionary term file refers to a folder or a…

Ask a question or leave a comment