closedir 


php128 apg

CLOSES a directory handle.



<?php

void closedir 
res $dir_handle = ? )


where,

$dir_handle The directory handle as resource
                      previously opened with opendir

?>

 $dir_handle 


The directory handle as resource previously opened with the function opendir.

If the $dir_handle is not specified, the last link opened by opendir is assumed.



  1 EXERCISE   

<?php

include('pathvalues.php');

$d01 P2TMPN 'xyz';
// P2TMPN exist
// xyz folder does not exist

echo $d01 '<br><br>';

if(
$h01 = @opendir($d01))
{
echo 
'[ ' $h01 ' ] Directory is open!<br><br>';
}
else
{
echo 
'Directory CANNOT be opened.<br><br>';
}

@
closedir($h01);

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PHP 7.4xx 
closes the actual directory handle
if opened by opendir

The @ before the name of the functions 
prevents the issue of errors 

You can test this by removing the @ 

PHP 8.O.xx
Fatal error is shown despite the 
@ before the function name       
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 

?>

 RESULT   

Directory CANNOT be opened

Probably does not exist!

You should test this code in your system with other directories.

The results may be different!


Additionally, this exercise in

PHP 8 will issue a
Fatal error: ...



  2 EXERCISE   

<?php

$dir02 
__DIR__ '/user/gfg/txt/';

$dir_handle02 = @opendir($dir02);
// OPENING A DIRECTORY

echo $dir02 '<br><br>'

if(
is_resource($dir_handle02)) 
{  
echo 
'Directory opened successfully.<br><br>'

@
closedir($dir_handle02); 
// CLOSING THE DIRECTORY

else

echo 
'Directory CANNOT be opened.<br><br>'
}  

?> 

 RESULT   

Directory CANNOT be opened

Probably does not exist!

You should test this code in your system with other directories.

The results may be different!


  3 EXERCISE   

<?php

include('pathvalues.php');

$dir03 P2TMPW 't1';

echo 
$dir03 '<br><br>';  

$opts03 = [ 'http' => [ 'method'=>"POST"
            
'header' => "Accept-language: en"
            
'content' => $dir03 ] ];  

$ctx03 stream_context_create($opts03); 


if(
$yn03 = @opendir($dir03$ctx03))
{
echo 
'[ ' $yn03 ' ] Directory is open!<br><br>';
var_dump($yn03);
echo 
'<br>Before closedir<br><br>';
}
else
{
echo 
'Directory CANNOT be opened.<br><br>';
}

closedir($yn03);

var_dump($yn03);
echo 
'<br>After closedir<br><br>';

?>

 RESULT   

[ Resource id #6 ] Directory is open!

resource(6) of type (stream)
Before closedir


resource(6) of type (Unknown)
After closedir


  4 EXERCISE   

<?php

@closedir();

/* - - - - - - - - - - - - - - - - - - - - -

    In PHP 7.4.xx
    closes the last directory
    opened by opendir
    
    In PHP 8.0.xx
    a Fatal error is shown despite 
    the @ placed at the beginning 
    of the function name
    
- - - - - - - - - - - - - - - - - - - - - - - */

?>

 RESULT   

PHP 7, no error will result,

however,

PHP 8 a Fatal error: ... will be displayed,
if there is no directory to close.


  5 EXERCISE   

<?php

$dir05 
__DIR__ '/temp/t1';

$dir_handle05 = @opendir($dir05);
// OPENING AN EXISTING DIRECTORY

echo $dir05 '<br><br><br>';  
  
if(
is_resource($dir_handle05)) 
{  
echo 
'Directory opened successfully.<br><br>';
var_dump($dir_handle05);    

else

echo 
'Directory CANNOT be opened.<br><br>'
}   

closedir();

/* - - - - - - - - - - - - - - - - - -

    closes the last directory
    opened by opendir

- - - - - - - - - - - - - - - - - - - - */

echo '<br><br>';
var_dump($dir_handle05);

?>

  6 EXERCISE   

<?php

include('pathvalues.php');
 
$dir2make06 P2TMPN 't3';

echo 
$dir2make06 '<br><br>';

if(!
file_exists($dir2make06))
{
@
mkdir ($dir2make06);

if(
file_exists($dir2make06))
{
echo 
'Directory created successfully<br><br>';

if(
$yn06 opendir($dir2make06))
{
echo 
'[ ' $yn06 ' ] Directory is open!<br><br>';
closedir($yn06);
var_dump($yn06);
echo 
'<br>After closedir<br><br>';
}
else
{
$yn06 null;
echo 
'Directory CANNOT be opened.<br><br>';

}
else
{
$yn06 null;
echo 
'Directory can not be created';
}
}
else
{
$yn06 null;
echo 
'Directory already exists 
                     and can not be recreated'
;
}

?> 

  7 EXERCISE   

<?php
 
$dir07 
=  __DIR__ '/adds';

echo 
$dir07 '<br><br>';

if(
file_exists($dir07))
{
echo 
'Directory already Exists.<br><br>';
$yn07 opendir($dir07);
echo 
'[ ' $yn07 ' ] Directory is open!<br><br>';
closedir($yn07);
var_dump($yn07);
echo 
'<br>After closedir.<br><br>';
}
else
{
echo 
'The directory must be created.<br><br>';
mkdir($dir07);
$yn07 opendir($dir07);
echo 
'[ ' $yn07 ' ] Directory created is open!<br><br>';
closedir($yn07);
var_dump($yn07);
echo 
'<br>After closedir.<br><br>';

}

?>