fclose
CLOSES an open file pointer.
This function should always terminate the use of the fopen function, so as not to keep any open handles.
This function returns TRUE on success or FALSE on failure.
<?php
bool fclose ( res $stream )
where,
$stream = A valid file pointer to a
successfully opened such as by the fopen function
?>
$stream
A valid file pointer to a successfully opened such as by the fopen or fsockopen functions.
EXERCISE
<?php
define('PATN', __DIR__ . '/temp/');
$file01 = PATN . 'blahblahblah.txt';
echo $file01 . '<br><br>';
$fp01 = fopen($file01, 'r', TRUE);
var_dump($fp01);
$yn01 = fclose($fp01);
if($yn01 == TRUE)
{
echo '<br><br>File pointer is closed as desired!<br><br>';
var_dump($fp01);
}
else
{
echo '<br><br>File pointer remains opened!<br><br>';
var_dump($fp01);
}
?>
RESULT
resource(6) of type (stream)
File pointer is closed as desired!
resource(6) of type (Unknown)
EXERCISE
<?php
define('PAW', 'file://' . __DIR__ . '/temp/');
$opts02 = [
'https'=>[
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
] ];
$context02 = stream_context_create($opts02);
/* - - - - - - - - - - - - - - - - - - - - - - - - - -
Sends an https request to www.example.com
with additional headers shown above
- - - - - - - - - - - - - - - - - - - - - - - - - - */
echo PAW . '<br><br>';
$fp02 = fopen(PAW . '/sw7.jpg', 'r',
true, $context02);
var_dump($fp02);
$ny = fclose($fp02);
if($ny == TRUE)
{
echo '<br><br>File pointer is closed as desired!<br><br>';
var_dump($fp02);
}
else
{
echo '<br><br>File pointer remains opened!<br><br>';
var_dump($fp02);
}
?>
RESULT
resource(10) of type (stream)
File pointer is closed as desired!
resource(10) of type (Unknown)
EXERCISE
<?php
// If PATH2N is not set Warnings
// will be issued in PHP 7.4.XX or
// a Fatal error in PHP 8.0.XX.
echo PATH2N . '<br><br>';
$file03 = PATH2N . 'blahblahbla.txt';
$fp03 = fopen($file03, 'r', TRUE);
var_dump($fp03);
$yn03 = fclose($fp03);
if($yn03 == TRUE)
{
echo '<br><br>File pointer is closed as desired!<br><br>';
var_dump($fp03);
}
else
{
echo '<br><br>File pointer remains opened or does not exist!<br><br>';
var_dump($fp03);
}
?>