Tutorial for Reading file on File Handling in PHP

What is Reading file on File Handling?

PHP has a number of functions for reading data from files. You may read entire file data, read data line by line, or read data character by character using various methods.

How many types of PHP file Read Functions are:-

The available PHP file read functions are given below.

  • fread()
  • fgets()
  • feof()

PHP readfile() Function

Reads a file and publishes it to the output buffer with the readfile() method.

Assume we have a text file on the server named “webdictionary.txt” that looks like this:

AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language

The following is the PHP code for reading the file and writing it to the output buffer (the readfile() method returns the number of bytes read if successful):

Example:-

If all you want to do is open a file and read its contents, the readfile() method comes in handy.

Tagged : / / / / / / / / / / / /