note 50960 added to function.glob

From: Date: Tue, 15 Mar 2005 01:05:18 +0000
Subject: note 50960 added to function.glob
Groups: php.notes 
Request: Send a blank email to php-notes+get-86445@lists.php.net to get a copy of this message
in the example below, i found i got an error if the directory was empty. <?php foreach (glob("*.txt") as $filename) { echo "$filename size " . filesize($filename) . "\n"; } ?> I think its because glob()'ing an empty directory returns false, and so calling foreach (false as $value) will obviously break. to fix this, i did the following: <?php $files = glob("*.txt) or array(); // give it an empty array if the directory is empty or glob fails otherwise echo "$filename size " . filesize($filename) . "\n"; } ?> Hope this helps someone ---- Manual Page -- http://www.php.net/manual/en/function.glob.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+50960 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50960&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50960&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50960&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50960&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50960&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+50960&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#86445) next »