note 103561 added to function.glob
| From: | mmm3567 at gmail dot com | Date: | Wed, 20 Apr 2011 14:34:56 +0000 |
| Subject: | note 103561 added to function.glob | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-179002@lists.php.net to get a copy of this message | ||
Something that's been covered before but that I just want to add on is glob() returning false.
If you have code like this...
<?php
$files = glob("*.php");
if(count($files) > 0)
{
foreach($files as $file)
{
// ...
}
}
else
{
// no matches
}
?>
... and an open_basedir restriction means that glob() returns false, this code will break, as $files
== false, and count(false) will return 1, which will mean it will execute the if() part of the
conditional instead of the else() part, and it will error at the foreach loop as $files is not an
array. I simply changed the conditional to this:
<?php
if(is_array($files) && count($files) > 0)
{
// ...
?>
That way, if $files is false, or it's an array which is empty, it'll run the code for no
matches, otherwise it'll loop through the results.
Obvious to most I'm sure but I didn't initially realise glob() was even returning false
for this very reason, and getting into the habit of checking if the result is an array will save
time and troubles.
----
Server IP: 92.48.74.199
Probable Submitter: 81.149.184.70
----
Manual Page -- http://www.php.net/manual/en/function.glob.php
Edit -- https://master.php.net/note/edit/103561
Del: integrated -- https://master.php.net/note/delete/103561/integrated
Del: useless -- https://master.php.net/note/delete/103561/useless
Del: bad code -- https://master.php.net/note/delete/103561/bad+code
Del: spam -- https://master.php.net/note/delete/103561/spam
Del: non-english -- https://master.php.net/note/delete/103561/non-english
Del: in docs -- https://master.php.net/note/delete/103561/in+docs
Del: other reasons-- https://master.php.net/note/delete/103561
Reject -- https://master.php.net/note/reject/103561
Search -- https://master.php.net/manage/user-notes.php