我从服务器加载文件:
$url = 'http://www.sample.com/test.xml';
$xml = simplexml_load_file($url);
如果服务器关闭,我会收到错误:
Warning: simplexml_load_file() [function.simplexml-load-file]: php_network_getaddresses: getaddrinfo failed:...
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity
如何检查文件是否到达?
答案 0 :(得分:2)
来自manual page for simplexml_load_file
返回类
SimpleXMLElement
的对象,其属性包含XML文档中保存的数据。出现错误时,它将返回FALSE
。
这意味着你可以做到
$url = 'http://www.sample.com/test.xml';
$xml = simplexml_load_file($url);
// check what was returned
if(FALSE === $xml) {
echo 'could not open file';
}