Hey...
I'm trying to do a little error catching when opening a directory. It works fine when the directory actually exists, but rather than have it bomb out if the directory is missing, I'd like my php to say something graceful, like sorry, not available. Using "or die" doesn't seem to do it, because by the time it gets there, it's already trying to open the path.
I call this function from my php like this:
$xmlfiles=get_xml_files($path) or die("not available");
where "get_xml_files" is a function that looks like this (first line is 23):
function get_xml_files($path){
$d = dir($path);
while($entry=$d->read()) { // loop through directory
if ($entry == ".") { echo ""; }
else if ($entry == "..") { echo ""; }
else if (strstr($entry, ".xml")) {
$choiceArray[]=$entry;
}
}
$d->close();
if (sizeof($choiceArray)==0) {$choiceArray[0]="No files available";}
return($choiceArray);
}
This yields an error like this:
Warning: OpenDir: No such file or directory (errno 2) in /foo/bazz/bat/php_functions.inc on line 24
Fatal error: Call to a member function on a non-object in /foo/bazz/bat/php_functions.inc on line 25
I tried to wrap it in a test like:
if($d = dir($path)){
This still gives me an error:
Warning: OpenDir: No such file or directory (errno 2) in /foo/bazz/bat/php_functions.inc on line 24
not available
So it's hitting the "die" part, but it's still trying to open the dir 🙁
How can I check to make sure a path is valid before I try and read it? I'm suspect there's an easy answer, but it's eluding me 🙁