Click to See Complete Forum and Search --> : trapping fopen errors


esm
09-11-2003, 08:43 PM
how do I trap the error when fopen fails

$file = fopen($url,'r');

if $url does not exist, I get an error

pyro
09-11-2003, 09:30 PM
You could do something like this, if you want to just ignore the error:$file = @fopen($url,'r');Or this, if you want to trap it, like you said:$file = fopen($url,'r') or die ("Couldn't open file");die() (http://us2.php.net/die) however, will terminate your script, so you may want to use the error supressor (@) and then check if $file contains any data...

esm
09-12-2003, 04:19 AM
Originally posted by pyro
you may want to use the error supressor (@) and then check if $file contains any data...

I wasn't quite sure how to do what you suggested but I did come up with the following which seems to work OK.


$file = @fopen($url,'r');
if (!empty($file)){
...process the file here.
}
else{ echo "URL does not exist";


hmmmm...now that I look at what you wrote above, what you suggested is exactly what I did.:D

THANKS...

pyro
09-12-2003, 06:53 AM
Yep, that looks good. You are welcome... :)