In this tutorials, we are going to learn how to read file in php.

php File Handling :

File handling is one of the most crucial needs of a dynamic system. Any web application must be able to handle files with ease.

File handling may be used to upload, download, create, read, and edit files.

Because of its necessity to the dynamic web applications, file handling must be performed with care to avoid damaging files.

Php provides readfile() function to read a file.

php readfile :

This function allows the system to read files and write it to a specified output buffer.

Let us create a text file called “sample.txt” as below.

 Sample php readfile example php tutorials php examples

Let us see how this works when we try to read using the readfile() function.

php readfile Example :

<?php
echo readfile("/home/chandrashekhar/Desktop/sample.txt");
?>

This simple line displays the contents of the file on the screen. You may notice that the text file is code for html.

The php readfile() function also returns the number of characters in the file.

Sample php read file example php tutorials php examples 56

Happy Learning 🙂