File Handling

File handling is an important part of any web application. You often need to open and process a file for different tasks. PHP File System allows us to create file, read file line by line, read file character by character, write file, append file, delete file and close file.

Php Open File - fopen()

The PHP fopen() function is used to open a file.

Syntax

Php Close File - fclose()

The PHP fclose() function is used to close an open file pointer.

Syntax

Example :-

Php Read File - fread()

The PHP fread() function is used to read the content of the file. It accepts two arguments: resource and file size.

Create a txt file "demo.txt"

Example :-

The PHP code to read the file and write it to the output buffer is as follows (the readfile() function returns the number of bytes read on success):