fclose Function - Close Opened File in PHP

How to close an opened file in php?


fclose() method is used to close a file which is already open.

To close a file using php
a) Call the function fclose with required arguments.
b) The argument should be the file name. The filename should be opened under the current (same) directory.

Example :

The below code will explain the function more precisely,
$file = "test.txt"; // Here we define the file path
$open = fopen($file, "r"); // Here we open the file in read mode using the php method fopen()

fclose($open); // Here we close the opened file using the function fclose()

In the above example, PHP automatically closes the file handled (i.e) test.txt, by the script.
If you are not using the fclose() method then the file you have opened will remain open for a longer than the time than it actually requires to be.
So, it is better to use the fclose() function to close your file after each call.

PHP Topics

Ask Questions

Ask Question