如果我们要判断远程文件是否存可以使用很多方法,在php中有ile_get_contents,curl, fsockopen,fopen这些函数都可以获取远程文件.
方法一,代码如下:
- <?php
- $url = “http:
- $fileExists = @file_get_contents($url, null, null, -1, 1) ? true:false;
- echo $fileExists;
- ?>
方法二,代码如下:
-
- function url_exists($url){
- $handle=curl_init($url);
- if(false===$handle){
- return false;
- }
- curl_setopt($handle,CURLOPT_HEADER,false);
- curl_setopt($handle,CURLOPT_FAILONERROR,true);
- curl_setopt($handle,CURLOPT_NOBODY,true);
- curl_setopt($handle,CURLOPT_RETURNTRANSFER,false);
- $connectable=curl_exec($handle);
-
- curl_close($handle);
- return $connectable;
- }
方法三,代码如下:
- function file_exists($url) {
- $curl = curl_init($url);
-
- curl_setopt($curl, CURLOPT_NOBODY, true);
-
- $result = curl_exec($curl);
- $found = false;
-
- if ($result !== false)
- {
- }
方法一无论图片在不在都是返回FALSE;
方法二windows下可行,LINUX下无论图片在不在都返加TRUE;
方法三应该是最合适的,代码如下:
- $url = "http://www.phpfensi.com";
- $info = parse_url($url);
- $fp = fsockopen($info['host'], 80,$errno, $errstr, 30);
- fputs($fp,"GET {$info['path']} HTTP/1.1rn");
- fputs($fp, "Host: {$info['host']}rn");
- fputs($fp, "Connection: closernrn");
- $headers = array();
- while(!feof($fp)) {
- $line = fgets($fp);
- if($line != "rn") {
- $headers[] = $line;
- }else {
- break;
- }
- }
-
- echo "<pre>";
- print_r($headers);
方法四,代码如下:
- <?php
- $url = 'http://www.phpfensi.com';
-
- if( @fopen( $url, 'r' ) )
- {
- echo 'File Exits';
- }
- else
- {
- echo 'File Do Not Exits';
- }
- ?>
|