note 50960 modified in function.glob by didou

From: Date: Mon, 15 Jan 2007 23:11:56 +0000
Subject: note 50960 modified in function.glob by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-121756@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 --was-- 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 http://php.net/manual/en/function.glob.php

« previous php.notes (#121756) next »