Closing File in PHP

Closing file in php - To close the file in PHP use the function fclose(). Each file opening process must end by closing the file, in order for the file to be locked. The fclose function refers to the function fopen and its parameters.
The way the fclose() function is written is as follows.
fclose ($handle);
To better understand it, here I give examples of the program.
Program
File Name: file07.php
Description: User application program counter with file.
Open notepad, type the following php code.
<?php
$counter_file="counter.txt";
if (!file_exists ($counter_file)) {
 fopen ($counter_file, "w"); 
 }
$file = fopen($counter_file,"r");

$counter = fread($file,10);
fclose($file);

$counter++;

echo "<h2>You are a visitor to - $counter</h2>";
$file = fopen($counter_file, "w");
fwrite($file,$counter);
fclose($file);
?>

Closing File in PHP

Save the php code with file07.php name in htdocs folder.
To run the program, open the browser then type http://localhost/file07.php in the address bar then enter. Then the result will look like the following.
Closing File in PHP
Program view

That's the explanation of my tutorial about closing file in php. Hope it helps you and can improve our understanding of the php programming language. Learn also tutorial about: How to Read File Content in PHP.

Comments