PHP File Handling: How to Open, Read and Close files using PHP

File handling is a very important aspect of the creation of Web applications.

PHP has various functions to handle files using which we can create, read, upload, or even edit files.

PHP has lots and lots of prebuilt functions to perform all of the major functions related to your files and folder.

You can check all of those PHP filesystem functions on this page.

However, here we are going to discuss some of the most useful features that is useful to handling files and folders using PHP.

PHP readfile() Function

The readfile() the function reads the whole file and outputs it if you choose to do so.

Here is how can you use this function:


a = readfile("filename.txt");

echo a;

So in this example, you see that we have a file named “filename.txt” using the function ‘readfile’ we read the whole file and assign the result to a variable named ‘a’.

And after that, we print ‘a’ using statement echo.

This whole code block will output all of the content inside our file ‘filename.txt’.

How to Open, Read and Close files using PHP?

PHP has functions like fopen(), read(), and fclose() which allows you to perform some basic file operations like Opening, closing, and reading files using PHP.

PHP Open file using fopen() function

The fopen() the function is just like readfile() is used to open files in PHP.

But ‘fopen’ is considered a better way to open your files since it offers more features and functionality.

Using this function we can not only open files but read, write, append, and more.

Here is the syntax for this function:

#First Open your file in Read only mode:
$myfile = fopen("myfilename.txt", "r");

#Read and output your file:
echo fread($myfile);

#Close your file:
fclose($myfile);

In the example above you notice that we not only use fopen but fread and fclose as well.

We first open the file then read it and once it has been done we close the file.

The second thing you have noticed is the second argument “r” in fopen() function.

Here r, refer to read-only mode.

Depending on our need we can open files in any mode available, here are all of them:

ModesDetails
rOpen File in read-only. Here pointer starts in the beginning
wOpen file in write-only. It erases the content or creates a new file if the file does not exist.
aOpen a file in write-only mode. It is used to append data at the end of the file.
xCreates a new file for write-only. If the file already exists return FALSE and error.
r+Open file for reading/writing mod.
w+Open file for read-write mod. erase the content or create a new file it does not exist.
a+Open file in reading and writing. It appends to an already existing file.
x+Creates a new file of reading and writing. Returns false if the file already exists.

PHP Read File using fread() function

‘fread’ function si for the reading files that we opened earlier.

This function contains two parameters first is the name of the file which we are trying to read and the second is the number of bytes to read.

And here is the syntax for this function:

fread($myfile, filesize($myfile));

How to Read single Charracter in PHP

Using function fgets() we can read a single character from a file.

Here is how to use this function:

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

How to Read Single line in PHP

Similar to reading file character by character, we also have function fgets() which reads a single line at a time.

Here is how fgets() is used:

$myfile = fopen("filename.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);

How to Check PHP End-Of-File

Using feof() we can if the EOF (end of file ) has been reached.

We can use this function to notify us when we reach the end of a file.

Here is how we can use this function with fgets():

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

PHP close file using fclose() function

We open files using fopen, we read using fread and we close files using fclose.

In programming closing, the file is as important as opening them.

Since we don’t want unessary opened files to take our server resources when we are done with them, we closed them.

Here is the syntax for this function:

fclose('filename.txt');

We close the file after opening it, so we at least open the file before closing them, otherwise, it will throw an error.

So in real life, the complete code will look like this:

#Opening the file
$myfile = fopen("filename.txt", "r");

#Close once we done with file
fclose($myfile);

Rahul Bodana passionately shares knowledge through tutorials on programming, investment, trading, gaming, and writing, aiming to empower others with valuable insights and how-to guides.