PDA

View Full Version : Should I use any error-checking when connecting to remote URL?


JJR512
07-16-2002, 09:02 AM
I have a hack (the Forum News (https://vborg.vbsupport.ru/showthread.php?s=&threadid=41052) hack) which uses file() to connect to a remote file to read it:
$data = implode("",file($filename));
...where $filename is a URL to an XML file on another server.

Right now, there is nothing in place to do anything about it if that remote file can't be found or connected to. It's possible that the remote server might go down, or the path or file name could be changed, or something else.

What happens if this PHP function, file(), can't get the remote file? Should I somehow check to see if the remote file can be connected to first, before the rest of the code tries to run? If so, how? Would @file() do anything?

Admin
07-16-2002, 09:14 AM
If you don't care what the error is, use this:
$data = @file($filename);
if ($data !== false) {
$data = implode('', $data);
} else {
// it failed
}

JJR512
07-17-2002, 02:20 AM
What would happen if I don't use that, and the script can't connect to the remote file?

Admin
07-17-2002, 12:48 PM
file() will return false and issue a notice. (or a warning, either one)

(is it that hard to test file('http://www.this.com/leads/to/no.where');?)