PHP fclose() Function
Last Updated :
11 Mar, 2024
The fclose() function in PHP is an inbuilt function that is used to close a file that 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 that 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 Exceptions:
- 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 work for remote files.It only works on files that are accessible by the server’s filesystem.
Example:
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
Examples of PHP fclose() Fucntion
Example 1:
php
<?php
$check = fopen ( "gfg.txt" , "r" );
fclose( $check );
?>
|
Output:
true
Example 2: In the below program the file named singleline.txt contains only a single line “This file consists of only a single line”.
php
<?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.
To know more about the PHP file Handiling please go through this article
Reference: http://php.net/manual/en/function.fclose.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...