简体   繁体   中英

php glob versus scandir

I encounter a peculiarity with the php glob function and wonder what the heck is going on.

<?php
$paths = glob('../test/*');
echo 'count = ' . count($paths) .'<br/>';
echo 'paths[0] = ' . $paths[0] .'<br/>';
echo 'scandir count = ' . count (scandir ('../test') );
?>

The test directory is empty and I get as result

count = 1
paths[0] = 
scandir count = 2

The scandir count of 2 I understand ( . and .. are counted too).
But I expected count of $paths to be 0, not 1.
And why, if it is 1, does $paths[0] have no value?

What has happened is the "glob" has returned false. This gives the output you've seen.

count = 1
paths[0] = 

Reading one report ( http://drupal.org/node/1157100 ) suggests that glob() can fail if you can't read the ../ parent directory, even though you can read ../test . I presume that scandir() doesn't have this restriction, and so it succeeds.

Solution in this case: give yourself permissions to the ../ directory as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM