opendir 


php128 apg

OPENS directory handle.





This function returns FALSE on failure.

If the path is not a valid directory or can not be opened due any restriction, also returns FALSE and an E_WARNING error level is generated, however, this error can be suppressed by prepending  @  to the front of the function name.



<?php

res
|false opendir ( str $path , res $context = ? )


where,

$path = The directory path to be opened

$context 
= stream context

?>

 $path 


The directory path to be opened.



 STREAM WRAPPERS 


This function attempts to open the directory especified by $path.


PROTOCOLS MEANING
file:// LOCAL File System
http:// URL HTTP
ftp:// URL FTP
php:// I/O streams
zlib:// Data Compression stream
data:// DATA (RFC-2397)
glob:// Find Filenames
phar:// PHP Archive
ssh2:// Secure Shell 2
rar:// RAR
ogg:// Audio
expect:// Interaction of stream processes
ed48

 $context 


For a description of the $context parameter, refer to the streams.



  1 EXERCISE   

<?php

$dir01 
= __DIR__ . '/temp/t1';

echo 
__DIR__ . '/temp/t1' . '<br><br>';

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

?>

  2 EXERCISE   

<?php

$dir02 
= 'temp/xyz';

echo 
__DIR__ . '/' . $dir02 . '<br><br>';

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

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The @ before the name of the function
prevents the issue of errors

You can test this by removing the @
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

?>

  3 EXERCISE   

<?php

define
('P2TEMPN', 'temp/t2/t2a');

$dir03 = P2TEMPN;

echo 
__DIR__ . '/' . P2TEMPN . '<br><br>';

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

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The @ before the name of the function
prevents the issue of errors

You can test this by removing the @ 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

?>

  4 EXERCISE   

<?php

define
('DIREP', __DIR__ . '/TEST/temp/t2/t2a');

$dir04 = DIREP;

echo 
DIREP . '<br><br>';

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

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -    
The @ before the name of the function
prevents the issue of errors

You can test this by removing the @       
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

?>

  5 EXERCISE   

<?php

$dir05 
= 'file://' .__DIR__ . '/data';

echo 
$dir05 . '<br><br>';

$opts05 = [ 'http' => [ 'method'=>"POST", 
                   
'header' => "Accept-language: en", 
                   
'content' => $dir05 ] ];  

$ctx05 = stream_context_create($opts05); 


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

?>

  6 EXERCISE   

<?php

$dir06 
= __DIR__ . '/user';

echo 
$dir06 . '<br><br>';

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

?>