File handling is an important part of any application whether it’s a desktop application or a web-based application. As we are learning web development using PHP, so in this tutorial, we will learn file handling in PHP.

How to read file in PHP?

PHP provides fopen() method to open and read file contents.

Fopen() function is alternative of php readfile() function and is loaded with more features.

The sample file, we will use for this PHP files tutorial is named my_dictionary.txt. Its contents are given below. The file contains some tech-related acronyms followed by their full forms.

ACID = Atomicity, Consistency, Isolation, Durability
ACL = Access Control List
ADC = Analog-to-Digital Converter
ADF = Automatic Document Feeder
ADSL = Asymmetric Digital Subscriber Line
AFP = Apple Filing Protocol
AGP = Accelerated Graphics Port
AIFF = Audio Interchange File Format
AIX = Advanced Interactive Executive
ALU = Arithmetic Logic Unit

fopen function accepts two parameters. The first parameter is the name of the file to open and the second parameter is the mode in which the file should be open such as read or write mode. If this function successfully opens the file, it returns its handle. In case of failure, you can print a message like in the following example.

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>
Note – files can be a source of memory leakage.

Keep yourself highly focused while working with files. Files are a crucial part of development. If you don’t have enough practice working with files, you may end up editing, removing, or filling files with garbage.

ModesDescription
rOpen a file for read-only. The file pointer starts at the beginning of the file
wOpen a file for write-only. Erases the contents of the file or creates a new file if it doesn’t exist. The file pointer starts at the beginning of the file
aOpen a file for write-only. The existing data in the file is preserved. The file pointer starts at the end of the file. Creates a new file if the file doesn’t exist
xCreates a new file for write-only. Returns FALSE and an error if the file already exists
r+Open a file for reading/write. The file pointer starts at the beginning of the file
w+Open a file for reading/write. Erases the contents of the file or creates a new file if it doesn’t exist. The file pointer starts at the beginning of the file
a+Open a file for reading/write. The existing data in the file is preserved. The file pointer starts at the end of the file. Creates a new file if the file doesn’t exist
x+Creates a new file for reading/write. Returns FALSE and an error if the file already exists
Modes of opening the file

How to get the size of the file in PHP?

filesize(filename) is the function that returns the size of the file. It accepts the path to the file along with its name and extension.

How to read a file in PHP?

fread() function reads the contents of an open file. This function accepts two parameters, the first parameter is the name of the file to read and the second parameter is the number of bytes to read from the file.

Example

fread($myfile,filesize("webdictionary.txt"));

How to close the opened file in PHP?

fclose() function is used to close an opened file. Many developers forget to close opened files which results in memory leakage and other errors. This function takes one parameter, which is the name of the file to close.

Example

<?php
$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>

How to read a single line from a file in PHP?

The function fread is described in the above section that reads the file starting from the location 0 to the size specified. However, the fgets() function reads a single line from the file.

Example

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>

How to check end of file in PHP?

feof() returns the end of the file. This function is useful because it can help to iterate the file content starting from the 0 indexes of the file, to the end which is in fact the end of the file.

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
  echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>

How to read the single file character from the file?

fgetc() function is used to read a single character from an opened file. Look at the example below.

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
  echo fgetc($myfile);
}
fclose($myfile);
?>

Remember that, calling fgetc function moves the file pointer to the next character. Similarly, calling the fgets function, the file pointer moves to the next line.

How to create a file in PHP?

fopen function is used to create a new file in PHP. Don’t be confused about the same function for opening and creating files. If the file to open does not exist, the fopen function creates and then opens the file in either reading or writing mode.

$myfile = fopen("testfile.txt", "w")

How to write in File in PHP?

fwrite function allows you to write something in the files. The first argument of the function is the name of the file to write and the second parameter is my string to write in the file.

<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?>

Complete reference of file handling in PHP 8 here.