PHP File Handling fopen fread and fclose Example

By | 2017-04-23T09:54:42+00:00 January 25th, 2017|PHP|0 Comments

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 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)
?>

[box type=”success” align=”alignleft” class=”” width=”100%”]
Sample php readfile example php tutorials php examples
[/box]

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.

[tie_list type=”checklist”]

  • 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.

[/tie_list]

php fread() :

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

[box type=”shadow” align=”alignleft” class=”” width=”100%”]

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

fclose () closes an open file.

fclose($myfile);

[/box]

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)
?>

[box type=”success” align=”alignleft” class=”” width=”100%”]

Sample php readfile example

[/box]

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)
?>

[box type=”success” align=”alignleft” class=”” width=”100%”]

Sample php readfile example
php tutorials
php examples

[/box]

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 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment