glob 


php128 apg

FINDS path names according certain pattern.





This function operates according to the rules used by libc glob, which is similar to the common shells.

For obvious reasons, this function does not work with remote files.



<?php

arr
|false glob ( str $pattern , int $flags = 0 ) 


where,

$pattern = The pattern to be matched

$flags 
= The valid flags to be used
              
( SEE the below TABLE )
  
?>

  $pattern   


The pattern to be matched.

Special characters to be used

* => Matches zero or more characters.

? => Matches exactly one character (any character).

[...] => Matches one character from a group of characters.
If the first character is !, matches any character not in the group.

\ => Escapes the following character, except when the GLOB_NOESCAPE flag is used.



  $flags   

VALUE CONSTANT MEANING DEFAULT
0 GLOB_MARK ADDS A SLASH TO EACH DIRECTORY RETURNED GLOB_MARK
1 GLOB_NOSORT DOES NOT ORDER FILES
2 GLOB_NOCHECK THE SEARCH PATTERN IF NO FILES MATCHING IT WERE FOUND
3 GLOB_NOESCAPE DO NOT SHOW ESCAPE SEQUENCES
4 GLOB_BRACE SEQUENCES BETWEEN BRACKETS WILL BE EVALUATED
5 GLOB_ONLYDIR ONLY DIRECTORY ENTRIES WHICH MATCH THE PATTERN
6 GLOB_ERR STOP ON READ ERRORS,
(LIKE UNREADABLE DIRECTORIES)
BY DEFAULT ERRORS ARE IGNORED
ed48


  EXTERNAL APPLICATION USING THIS FUNCTION   

Videos for all  !




  1 EXERCISE   

<?php

$pt01 
= __DIR__ . '/temp/*';

echo 
$pt01 . '<br>';

$tst01 = glob($pt01, 0);

echo 
'<pre>';
print_r($tst01);
echo 
'</pre>'; 

?>

  2 EXERCISE   

<?php

$lk02a 
= __DIR__ . '/temp*';

$lk02b = __DIR__ . '/temp/*';

echo 
$lk02a . ' => GLOB_ONLYDIR<br>';

$tst02a = glob($lk02a, GLOB_ONLYDIR);

echo 
'<pre>';
print_r($tst02a);
echo 
'</pre><br>'; 

echo 
$lk02b . ' => GLOB_ONLYDIR<br>';

$tst02b = glob($lk02b, GLOB_ONLYDIR);

echo 
'<pre>';
print_r($tst02b);
echo 
'</pre>'; 

?>

  3 EXERCISE   

<?php

$lk03 
= __DIR__ . '/temp/';

echo 
$lk03 . '{} => GLOB_BRACE<br>';

$tst03a = glob($lk03 . "{}", GLOB_BRACE);

echo 
'<pre>';
print_r($tst03a);
echo 
'</pre><br>'; 

echo 
$lk03 . '{*} => GLOB_BRACE<br>';

$tst03b = glob($lk03 . "{*}", GLOB_BRACE);

echo 
'<pre>';
print_r($tst03b);
echo 
'</pre>'; 

?>

  4 EXERCISE   

<?php

$lk04 
= __DIR__ . '/temp/{*.m4a}';

echo 
"$lk04 => GLOB_BRACE<br><br>";

$tst04 = glob($lk04, GLOB_BRACE);

echo 
'<pre>';
print_r($tst04);
echo 
'</pre>'; 

?>

  5 EXERCISE   

<?php 

$pt05 
= __DIR__ . '/*{.PHP,.php}'; 

echo 
"$pt05 => GLOB_BRACE<br><br>";

$tst05 = glob($pt05, GLOB_BRACE );

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
   Between the brackets we must provide 
   the patterns to be shown in the 
   directory pointed by __DIR__
   
   Here, as example we use:
   .PHP and .php
   
   In your case, surely, it may be different
   
   Try it!
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */  

foreach($tst05 as $ts05)
{
echo 
$ts05 . '<br>';
}    

?> 

  6 EXERCISE   

<?php

define 
('PMUS','H:/WEB/public_html/path to/');

$p06 = PMUS;

echo 
PMUS . "*{.mp3,.MP3,.wav,.WAV,.m4a,.M4A} => GLOB_BRACE<br><br>";

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      PMUS must indicate the path for
      .mp3, .wav and .m4a files 
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$tst06 = glob($p06  . 
"*{.mp3,.MP3,.wav,.WAV,.m4a,.M4A}", GLOB_BRACE); 

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   This code retrieves all files in the 
   specified directory, if they are:
   .mp3, .MP3, .wav, .WAV, .m4a, .M4A  
    - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

foreach($tst06 as $ts06)
{
echo 
$ts06 . '<br>';
}    

?> 

  7 EXERCISE   

<?php
 
$dir2make07 
= __DIR__ . '/temp/t3';
 
if(!
file_exists($dir2make07))
{
@
mkdir ($dir2make07);

if(
file_exists($dir2make07))
{
echo 
'Directory created successfully';
}
else
{
echo 
'Directory can not be created';
}
}
else
{
echo 
'Directory already exists and can not be recreated';
}

$pt07 = __DIR__ . '/temp/t3';

$tst07 = glob($pt07);

echo 
'<pre>';
print_r($tst07);
echo 
'</pre>'; 

$pt07a = 'temp/t1*';

$tst07a = glob($pt07a);

echo 
'<pre>';
print_r($tst07a);
echo 
'</pre>';

?>

  8 EXERCISE   

<?php

$lk08 
= __DIR__ . '/';

$p08 = $lk08 . 'temp/*' . '[.txt, .jpg]';

$tst08 = glob($p08, GLOB_BRACE); 

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   This code retrieves all files in the 
   specified directory, if they are:
   .txt and .jpg  
    - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

foreach($tst08 as $ts08)
{
echo 
$ts08 . '<br>';
}    

?>