Need help with filesystem stuff

I don’t get the PHP filesystem functions… I got some help yesterday here,

this is a follow-up…

am getting very frustrated b/c I don’t have internet access during the day (this is a personal project) and so can’t search for stuff like I need to… I did manage to download docs so I have a local version of docs, but of course you can’t search it (can’t search for things like “get length of array”, for example…)

first and foremost need to:

  1. list ONLY directories in dir I’m opening
  2. filter out parent and g’parent dirs, i.e., any ‘upstream’ dirs… how do I do that? in Java ‘upstream’ dirs are never listed… was not expecting this… need to filter them out of list of files in dir…

wanted to attach screenshot of dir I’m working with… don’t see how to attach files… at any rate, contains “page1”… thru “page6” plus a file created by the mac all over the place, called .DS_store, plus a file called test.txt, that I included for testing purposes…

so from docs:

if (is_dir($dirPgs)) {
    if ($dh = opendir($dirPgs)) {
        while (($file = readdir($dh)) !== false) {  // this just means if dir is not empty, right?
            if (is_dir($file)) echo $file . ' -- this file is a dir<br>';

this only prints:

. -- this file is a dir
.. -- this file is a dir

i.e., it only reads the two upstream dirs as dirs!!! it doesn’t recognize “page1”, “page2” etc… as dirs… why on earth is this…

  1. function ‘glob()’, that someone suggested I use yesterday in other thread, is not getting me anywhere…
    it says in docs that you don’t need to use opendir() if you use this function… so how do you access the dir you want?

in example in docs for glob() fn

foreach (glob("*.txt") as $filename) {
    echo "$filename size " . filesize($filename) . "\n";

it doesn’t show you how to access dir…
(& what is diff betw using this and using standard

if ( strpos($file, 'page') == true) to filter out file-names?)

I didn’t know how else to access dir, so I did:

  if (is_dir($dirPgs)) {
        if ($dh = opendir($dirPgs)) {
        foreach (glob("page*") as $filename) {
            echo 'this is a page dir<br>';

it prints nothing…

still don’t get why this doesn’t work…

if ( strpos($file, 'page') == true) echo $file . ' -- this is a page dir<br>';`

it prints nothing… while this…

if ( strpos($file, 'age') == true) echo $file . ' -- this is a page dir<br>';


page1 -- this is a page dir
page2 -- this is a page dir
page3 -- this is a page dir
page4 -- this is a page dir
page5 -- this is a page dir
page6 -- this is a page dir

this doesn’t make any sense…

this also prints nothing:

 if ( strpos($file, 'p') == true) echo $file . ' -- this is a page dir<br>';

I don’t get this… why doesn’t it read the 1st char in file name?

also looked @ fn file()…
( array file ( string $filename); )

so I did:

$arrayOfFiles file( $dirPgs );

this shows a PHP syntax error in my IED…
now the examples for this file() fn only show how to get contents of a file (how many lines in a file, size of file, etc…) can you not use this file() fn to list files in a dir?

man, this is so simple in Java… I never had a hard time in Java with this…

here’s Java code for what I want to do in PHP:

File dirPgs = new File(FPathPgs);
File[] pagesDirs = dirPgs.listFiles();  
for (int i=0; i < pagesDirs.length; i++) {    
    if (pagesDirs[i].isDirectory() ) {
        if (pagesDirs[i].getName().indexOf("page") != -1) {
        //    (use vector since you cannot add elements to arrays, later convert to array)

etc… it seems so simple in Java…:wink:

would very much appreciate some help… thank you…


foreach(glob("my/path/to/dir/*",GLOB_ONLYDIR) AS $filename) {
  echo $filename."<br />";


foreach(glob($dirPgs,GLOB_ONLYDIR) AS $filename) {
        echo $filename . "<br />";

it prints this:


i.e., it just prints path to dir I’m in, not the files inside the dir… (sigh…:wink:
i.e., it prints what var $dirPgs evaluates to… which is the path to the dir…

oh brother…:frowning:

thank you…

Did you put the * on the end?

Also, you specifically said you were looking for directories, not files.

well, if I do


I get syntax error… so can only put literal to path?

ok, so did:

foreach(glob("/Library/WebServer/Documents/photos/section1/*",GLOB_ONLYDIR) as $filename) {
      echo $filename." <br />";

it prints:


how do I get just the names of the files? (not the full path to them)?

getting somewhere…:wink: thank you…

So you want to append the string to the variable. In php concatenation is done with a period, so


Would append a literal * to the string.
For the name, basename($filename) should return the last piece.
Alternatively, str_replace($dirPgs,“”,$filename) will get you everything that comes after the end of your directory.

now in this example…

foreach (glob("*.txt") as $filename) {
    echo "$filename size " . filesize($filename) . "\n";

how does it tell it where to find said “.*txt” files???

thank you…

oh my gosh… this finally did it…

foreach(glob($dirPgs."*",GLOB_ONLYDIR) as $filename) {
              echo basename($filename) ."<br />";



now is it documented here, (or anywhere) how to get only filenames (not entire paths)?? it seems like something very elemental…:wink:

this has been a little bit like pulling teeth…:wink:

thank you very much… I appreciate your help…

If… I recall correctly glob executes at local directory if an absolute path is not used, so *.txt would find all text files at or below the directory that the file executing the glob is running from.

I’m not at a computer that I can test this from at the moment.

You can browse to to get the manual page for that function. Try it with basename. The manual lists similar functions on the right hand side (in this case, file and directory functions) so you can see what things may be related.

PS: how do I get how many files (how many dirs that match my set? – i.e., how many dir-names that start with “page”?)

thank you…

Instead of globbing in the for each, store the output of glob (which is an array) in a variable. The number of elements in the array is measured by [fphp]count/fphp.

actually, I got it…

$noOfPgDirs = 0;
$files = glob($dirPgs."*",GLOB_ONLYDIR);
if ($files){
    echo 'no. of page dirs -- ' . $noOfPgDirs = count($files) . "<br>";

from here,

thank you very much…

if ( strpos($file, ‘page’) == true) {
echo $file . ’ – this is a page dir’;
// it prints nothing…
/// I don’t get this… why doesn’t it read the 1st char in file name?

It does read the first character and returns a zero(0) value,
unfortunately zero(0) is interpreted as FALSE;

// while this…
if ( strpos($file, ‘age’) == true){
echo $file . ’ – this is a page dir’;

The value returned is 1 which is interpreted as TRUE.


If you want to display a list of directories within a directory…

Try this:

$dirPgs = '.';
$d      = dir($dirPgs); // returns '.', '..', $file or a directory 

$aFiles = array();
$aDirs  = array();
while (false !== ($entry = $d->read())) {

    // prevent '.' && '..'
    if('.' === substr($entry,0,1) ) { 
        // do nothing
        // did you notice the TREBLE equals?

        if( is_dir($entry) ) {
            $aDirs[] = $entry;
            $aFiles[] = $entry;

_show($aDirs,  'Directories');

_show($aFiles, 'Files');

function _show($val, $title=NULL)
    echo $title;
    echo '<pre>';
    echo '</pre>';