国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP使用get_headers函數(shù)判斷遠(yuǎn)程文件是否存在的方法

2024-09-04 11:42:56
字體:
供稿:網(wǎng)友

這篇文章主要介紹了PHP使用get_headers函數(shù)判斷遠(yuǎn)程文件是否存在的方法,以實(shí)例形式分析了使用get_headers函數(shù)對(duì)遠(yuǎn)程文件是否存在進(jìn)行判斷的方法,以及針對(duì)重定向的排除方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了PHP使用get_headers函數(shù)判斷遠(yuǎn)程文件是否存在的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

以前講過程關(guān)于php判斷遠(yuǎn)程文件是否存在的文章都是利用fopen,sockt,curl函數(shù)來實(shí)現(xiàn)檢查遠(yuǎn)程文件是否存在,下面我再介紹利用 get_headers來檢查遠(yuǎn)程文件是否存在,感興趣的朋友可以參考一下。

先來簡(jiǎn)單了解get_headers()函數(shù)

get_headers() 返回一個(gè)數(shù)組m包含有服務(wù)器響應(yīng)一個(gè) HTTP 請(qǐng)求所發(fā)送的標(biāo)頭。

get_headers:發(fā)送服務(wù)器響應(yīng)HTTP請(qǐng)求

get_headers(字符串url[鏈接格式])

get_headers()以數(shù)組的形式返回服務(wù)器HTTP請(qǐng)求m如果執(zhí)行失敗,將返回FALSE和一個(gè)錯(cuò)誤的水平E_WARNING,可選參數(shù)設(shè)置為1,get_headers()能分析系統(tǒng)的響應(yīng)速度和集數(shù)組中的鍵,注意:使用該函數(shù)需要把 php.ini里面的allow_url_fopen = On,才能使用

實(shí)例代碼如下:

  1. <?php 
  2. $url = 'http://www.survivalescaperooms.com'
  3. print_r(get_headers($url)); 
  4. print_r(get_headers($url, 1)); 
  5. ?> 

運(yùn)行結(jié)果如下:

  1. Array 
  2.     [0] => HTTP/1.1 200 OK 
  3.     [1] => Cache-Control: max-age=1800 
  4.     [2] => Content-Length: 54874 
  5.     [3] => Content-Type: text/html 
  6.     [4] => Content-Location: https://www.survivalescaperooms.com/index.htm 
  7.     [5] => Last-Modified: Fri, 28 Nov 2014 03:34:56 GMT 
  8.     [6] => Accept-Ranges: bytes 
  9.     [7] => ETag: "b66ba847bcad01:bc5" 
  10.     [8] => Server: Microsoft-IIS/6.0 
  11.     [9] => Date: Fri, 28 Nov 2014 03:37:34 GMT 
  12.     [10] => Connection: close 
  13. Array 
  14.     [0] => HTTP/1.1 200 OK 
  15.     [Cache-Control] => max-age=1800 
  16.     [Content-Length] => 54874 
  17.     [Content-Type] => text/html 
  18.     [Content-Location] => https://www.survivalescaperooms.com/index.htm 
  19.     [Last-Modified] => Fri, 28 Nov 2014 03:34:56 GMT 
  20.     [Accept-Ranges] => bytes 
  21.     [ETag] => "b66ba847bcad01:bc5" 
  22.     [Server] => Microsoft-IIS/6.0 
  23.     [Date] => Fri, 28 Nov 2014 03:37:35 GMT 
  24.     [Connection] => close 

判斷遠(yuǎn)程文件是否存在代碼如下:

  1. //判斷遠(yuǎn)程文件是否存在   
  2. function remote_file_exists($url) {   
  3.         $executeTime = ini_get('max_execution_time');   
  4.         ini_set('max_execution_time', 0);   
  5.         $headers = @get_headers($url);   
  6.         ini_set('max_execution_time'$executeTime);   
  7.         if ($headers) {   
  8.             $head = explode(' '$headers[0]);   
  9.             if ( !emptyempty($head[1]) && intval($head[1]) < 400) return true;   
  10.         }   
  11.         return false;   

排除重定向的實(shí)例代碼如下:

  1. <?php  
  2. /** 
  3.  * Fetches all the real headers sent by the server in response to a HTTP request without redirects 
  4.  * 獲取不包含重定向的報(bào)頭 
  5.  */  
  6.      
  7. function get_real_headers($url,$format=0,$follow_redirect=0) {  
  8.   if (!$follow_redirect) {  
  9.     //set new default options  
  10.     $opts = array('http' =>  
  11.         array('max_redirects'=>1,'ignore_errors'=>1)  
  12.     );  
  13.     stream_context_get_default($opts);  
  14.   }  
  15.   //get headers  
  16.     $headers=get_headers($url,$format);  
  17.     //restore default options  
  18.   if (isset($opts)) {  
  19.     $opts = array('http' =>  
  20.         array('max_redirects'=>20,'ignore_errors'=>0)  
  21.     ); 
  22.     stream_context_get_default($opts);  
  23.   }  
  24.   //return  
  25.     return $headers;  
  26. }  
  27. ?> 

希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阜平县| 石棉县| 鞍山市| 白沙| 浮山县| 新绛县| 英吉沙县| 阜南县| 平湖市| 民县| 宝兴县| 修文县| 红桥区| 延吉市| 闸北区| 岐山县| 宜阳县| 清苑县| 犍为县| 东安县| 漯河市| 石家庄市| 定西市| 鹰潭市| 巫山县| 临城县| 溧水县| 阿拉善右旗| 饶阳县| 永和县| 凤庆县| 永昌县| 班戈县| 师宗县| 明星| 车险| 南漳县| 易门县| 青州市| 乌海市| 辰溪县|