【问题标题】:Checking existence of a remote file using PHP使用 PHP 检查远程文件是否存在
【发布时间】:2012-07-26 08:29:49
【问题描述】:

我正在尝试找到一个最佳解决方案来保存 性能、内存使用情况 等,以检查文件是否存在于不同的域中。在我的例子中,该文件是一个 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";
}

【问题讨论】:

标签: php xml curl fopen


【解决方案1】:
$opts = array('http' =>
  array(
    'method'  => 'HEAD'
  )
);

$context  = stream_context_create($opts);

$result = fopen('http://example.com/submit.php', 'rb', false, $context);

Example taken (but shortened) from the manual

然后使用stream_get_meta_data() 获取响应头,类似于

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

【讨论】:

  • 正如 Dow 所说,cURL 更快。 here 也有测试运行。
  • 即使我可以(稍微)重现该行为,我也不会将一些未注释的数字命名为“测试”。在我看来,流更容易处理。 “性能”不是一切
猜你喜欢
  • 2010-11-02
  • 1970-01-01
  • 2011-07-24
  • 2010-11-15
  • 1970-01-01
  • 1970-01-01
  • 2012-05-16
  • 1970-01-01
相关资源
最近更新 更多