The fclose() function in PHP is an inbuilt function which is used to close a file which is pointed by an open file pointer. The fclose() function returns true on success and false on failure. It takes the file as an argument which has to be closed and closes that file.
Syntax:
bool fclose( $file )
Parameters: The fclose() function in PHP accepts only one parameter which is $file. This parameter specifies the file which has to be closed.
Return Value: It returns true on success and false on failure.
Errors And Exception:
- A file has to be closed first using the fclose() function if it has been written via fwrite() function and you have to read the contents of the file.
- The fclose() function in PHP doesn’t works for remote files.It only works on files which are accessible by the server’s filesystem.
Examples:
Input : $check = fopen("gfg.txt", "r");
fclose($check);
Output : true
Input: $check = fopen("singleline.txt", "r");
$seq = fgets($check);
while(! feof($check))
{
echo $seq ;
$seq = fgets($check);
}
fclose($check);
Output:true
Below programs illustrate the fclose() function:
Program 1:
<?php
$check = fopen ( "gfg.txt" , "r" );
fclose( $check );
?>
|
Output:
true
Program 2: In the below program the file named singleline.txt contains only a single line “This file consists of only a single line”.
<?php
$check = fopen ( "singleline.txt" , "r" );
$seq = fgets ( $check );
while (! feof ( $check ))
{
echo $seq ;
$seq = fgets ( $check );
}
fclose( $check );
?>
|
Output:
This file consists of only a single line.
Reference:
http://php.net/manual/en/function.fclose.php
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
26 Jun, 2023
Like Article
Save Article