readdir 


php128 apg

READS entry from directory handle.



<?php

str
|false readdir ( 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 opendir.



  1 EXERCISE   

<?php

$d01 
= __DIR__ . '/temp/';

if(
$hdl01 = opendir($d01))
{
echo 
'[ ' . $hdl01 . ' ] Directory is open!<br><br>';
$rd01 = readdir($hdl01);
while(
false !== ($rd01 = readdir($hdl01) ))
{
if(
$rd01 != '.' && $rd01 != '..')
{
echo 
$rd01 . '<br>';
}
}
closedir($hdl01);
echo 
'<br>';        
}
else
{
echo 
'Directory CANNOT be opened.<br><br>';
}

?>

 RESULT   

[[ Resource id #10 ] Directory is open!

blahblahblah.txt
love.txt
sete anões.txt
sw7.jpg
t1
t2
Your Wildest Dreams.m4a



You must test in your system with an existing directory.

The results, certainly, will be different!

Note that the one file name used here has an accentuated character.

This may pose a problem in systems that use only the English language.

  2 EXERCISE   

<?php

$d02 
= 'file://' . __DIR__ . '/temp/';
// STREAM WRAPPER

if($hdl02 = opendir($d02))
{
echo 
'[ ' . $hdl02 . ' ] Directory is open!<br><br>';
$rd02 = readdir($hdl02);
while(
false !== ($rd02 = readdir($hdl02) ))
{
if(
$rd02 != '.' && $rd02 != '..')
{
echo 
$rd02 . '<br>';
}
}
closedir($hdl02);
echo 
'<br>';        
}
else
{
echo 
'Directory CANNOT be opened.<br><br>';
}

?>

 RESULT   

[[ Resource id #10 ] Directory is open!

blahblahblah.txt
love.txt
sete anões.txt
sw7.jpg
t1
t2
Your Wildest Dreams.m4a



You must test in your system with an existing directory.

The results, certainly, will be different!

Note that the one file name used here has an accentuated character.

This may pose a problem in systems that use only the English language.


The same results as the previous exercise.

  3 EXERCISE   

<?php

$d03 
= 'temp/t1';

if(
$hdl03 = opendir($d03))
{
echo 
'[ ' . $hdl03 . ' ] Directory is open!<br><br>';
$rd03 = readdir($hdl03);
while(
false !== ($rd03 = readdir($hdl03) ))
{
if(
$rd03 != '.' && $rd03 != '..')
{
echo 
$rd03 . '<br>';
}
}
closedir($hdl03);
echo 
'<br>';        
}
else
{
echo 
'Directory CANNOT be opened.<br><br>';
}

?>