Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

php - Check if file exists on remote machine

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

I now have this function:

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;
}

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...)

Any ideas?

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...