检查远程机器上是否存在文件 [英] Check if file exists on remote machine

查看:127
本文介绍了检查远程机器上是否存在文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用php检查远程网络服务器上是否存在文件。

I want to check if a file exists on a remote webserver with php.

我现在有这个功能:

function url_exists($url) {
   // Version 4.x supported
   $handle   = curl_init($url);
   if (false === $handle)
   {
       return false;
   }
   curl_setopt($handle, CURLOPT_HEADER, false);
   curl_setopt($handle, CURLOPT_FAILONERROR, true);  // this works
   curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); 
    // request as if Firefox    
    curl_setopt($handle, CURLOPT_NOBODY, true);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
    $connectable = curl_exec($handle);
    curl_close($handle);
    return $connectable;
}

它工作正常,但如果我通过一个ip地址,它返回false ..
(所以我想检查 http://123.456.789.121/test.jpg ,当我发送 http://somedomain.com/test.jpg 时,它工作正常。 。)

It works fine, but if I pass an ip address instead of a domain name it returns false.. (so I want to check http://123.456.789.121/test.jpg, when I send http://somedomain.com/test.jpg it works fine...)

任何想法?

提前感谢!

推荐答案

远程服务器可能使用 Host 头解析文件。

如果是,使用域名。

The remote server probably resolves files using the Host header.
If so, you need to use a domain name.

您可以明确传送 Host 标头至IP位址,但我不建议。

You may be able to explicitly pass a Host header to the IP address, but I wouldn't recommend it.

这篇关于检查远程机器上是否存在文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆