PHP

php readfile Example Tutorials

In this tutorial, 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.txt
 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 :)

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

Ask a question or leave a comment