Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP检查远程文件的存在性_Php_Xml_Curl_Fopen - Fatal编程技术网

使用PHP检查远程文件的存在性

使用PHP检查远程文件的存在性,php,xml,curl,fopen,Php,Xml,Curl,Fopen,我正试图找到一个最佳的解决方案,从性能、内存使用等方面进行保存,以检查文件是否存在于不同的域中。在我的例子中,文件是XML,大小可以介于10KB到10MB之间 以下哪一项最适合使用?如果你有更好的方法,我很乐意用它来代替 谢谢 卷曲 $ch = curl_init("http://www.example.com/hello.xml"); curl_setopt($ch, CURLOPT_NOBODY, true); curl_exec($ch); $retcode = curl_getinfo

我正试图找到一个最佳的解决方案,从性能、内存使用等方面进行保存,以检查文件是否存在于不同的域中。在我的例子中,文件是XML,大小可以介于10KB到10MB之间

以下哪一项最适合使用?如果你有更好的方法,我很乐意用它来代替

谢谢

卷曲

$ch = curl_init("http://www.example.com/hello.xml");

curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// $retcode > 400 -> not found, $retcode = 200, found.
curl_close($ch);
FOPEN

$url = "http://www.example.com/hello.xml";

if (@fopen($url, "r")) {
   echo "File Exists";
} else {
   echo "Can't Connect to File";
}

然后使用获取响应头,如

$meta = stream_get_meta_data($result);
var_dump($meta['wrapper_data']);

卷曲更快。参见XML文件的例子为什么不
simplexml\u load\u file
?正如陶氏在上面的评论中所说,cURL更快。还有一个测试运行。即使我可以(稍微)重现这种行为,我也不会将一些未注释的数字命名为“测试”。在我看来,溪流更容易处理。“表现”不是一切
$meta = stream_get_meta_data($result);
var_dump($meta['wrapper_data']);