scandir 


php128 apg

LIST files and directories inside the specified path.





This function returns an ARRAY with the listed filenames on success or FALSE on falilure.

If directory is not a directory, then FALSE is returned, and an error of level E_WARNING is generated.



<?php

arr scandir 
( str $directory , 
                     
int $sorting_order = SCANDIR_SORT_ASCENDING , 
                     
res $context = ? )


where,

$directory = The directory to be scaned

$sorting_order 
= The sorting order to show the files
                          
( SEE the below TABLE )

$context = The stream context                  

?>

  $directory   


The directory to be scaned.



  $sorting_order   

CONSTANT VALUE MEANING
SCANDIR_SORT_ASCENDING 0 SET the alphabetical order ASCENDING
SCANDIR_SORT_DESCENDING 1 SET the alphabetical order DESCENDING
SCANDIR_SORT_NONE 2 UNSORTED
Since PHP 5.4.0
ed48


  $context   


The STREAM CONTEXT.



  1 EXERCISE   

<?php


define 
("PATH2TPW", 'file://' . __DIR__ . '/temp/');

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

   This path, (stream wrapper), 
   must be included as an include file 
   to be used correctly
   
   It will be used constantly from now on

   - - - - - - - - - - - - - - - - - - - - - - - - - */
   
$d01 = PATH2TPW;

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

// SET the alphabetical order DESCENDING
$f01 = scandir($d01, 1);

foreach( 
$f01 as $v01 )
{
    echo 
$v01 . '<br>';
}

?>

  2 EXERCISE   

<?php

define 
("PATH2TPN", __DIR__ . '/temp/');

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

   This path, 
   must be included as an include file 
   to be used correctly
   
   It will be used constantly from now on

   - - - - - - - - - - - - - - - - - - - - - - - - - */
   
$d02 = PATH2TPN;

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

//  SET the alphabetical order ASCENDING
$f02 = scandir($d02, 0);

foreach( 
$f02 as $v02 )
{
    echo 
$v02 . '<br>';
}

?>

  3 EXERCISE   

<?php

define 
("PATH2TPW", 'file://' . __DIR__ . '/temp/');

$d03 = PATH2TPW;
// MUST BE DEFINED AS STREAM WRAPPER

$o03 = [ 'http' => [ 'method'=>"POST",  
          
'header' => "Accept-language: en",
          
'user' => "User Name",          
          
'content' => $d03 ] ]; 

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

$c03 = stream_context_create($o03);  
  

//  UNSORTED
$f03 = scandir($d03, SCANDIR_SORT_NONE, $c03);

foreach( 
$f03 as $v03 )
{
    echo 
$v03 . '<br>';
}

?>