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

首頁 > 語言 > PHP > 正文

PHP get_headers函數判斷遠程文件是否存在

2024-09-04 11:48:46
字體:
來源:轉載
供稿:網友

以前我有講過程關于php判斷遠程文件是否存在的文章,那里都介紹利用fopen,sockt,curl函數來實現檢查遠程文件是否存在了,下面我再介紹利用 get_headers來檢查遠程文件是否存在,有需要了解的朋友可參考.

先來簡單了解get_headers()函數

get_headers() 返回一個數組m包含有服務器響應一個 HTTP 請求所發送的標頭。

get_headers:發送服務器響應HTTP請求

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

get_headers()以數組的形式返回服務器HTTP請求m如果執行失敗,將返回FALSE和一個錯誤的水平E_WARNING》,

可選參數設置為1,get_headers()能分析系統的響應速度和集數組中的鍵,

注意:使用該函數需要把 php.ini里面的allow_url_fopen = On,才能使用

例,代碼如下:

  1. <?php 
  2. $url = 'http://www.survivalescaperooms.com'
  3. print_r(get_headers($url)); 
  4. print_r(get_headers($url, 1)); 
  5. ?> 
  6. 返回值 
  7. Array 
  8.     [0] => HTTP/1.1 200 OK 
  9.     [1] => Date: Sat, 29 May 2004 12:28:13 GMT 
  10.     [2] => Server: Apache/1.3.27 (Unix)  (Red-Hat/Linux) 
  11.     [3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT 
  12.     [4] => ETag: "3f80f-1b6-3e1cb03b" 
  13.     [5] => Accept-Ranges: bytes 
  14.     [6] => Content-Length: 438 
  15.     [7] => Connection: close 
  16.     [8] => Content-Type: text/html 
  17. Array 
  18.     [0] => HTTP/1.1 200 OK 
  19.     [Date] => Sat, 29 May 2004 12:28:14 GMT 
  20.     [Server] => Apache/1.3.27 (Unix)  (Red-Hat/Linux) 
  21.     [Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT 
  22.     [ETag] => "3f80f-1b6-3e1cb03b" 
  23.     [Accept-Ranges] => bytes 
  24.     [Content-Length] => 438 
  25.     [Connection] => close 
  26.     [Content-Type] => text/html 

例,代碼如下:

  1. //判斷遠程文件是否存在   
  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;   
  12.  } 

例2,排除重定向的例子,代碼如下:

  1. <?php  
  2. /** 
  3.  * Fetches all the real headers sent by the server in response to a HTTP request without redirects 
  4.  * 獲取不包含重定向的報頭 
  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.     ); //開源軟件:Vevb.com 
  22.     stream_context_get_default($opts);  
  23.   }  
  24.   //return  
  25.     return $headers;  
  26. }  
  27. ?>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德化县| 莆田市| 武清区| 高平市| 白朗县| 玉环县| 聊城市| 高邮市| 花莲市| 当阳市| 南雄市| 宜君县| 屯门区| 轮台县| 黔西| 盘锦市| 佳木斯市| 措勤县| 开封县| 连云港市| 嵊州市| 芷江| 平顶山市| 金山区| 淳安县| 开化县| 高要市| 福海县| 长丰县| 双鸭山市| 屯昌县| 保德县| 柳州市| 汽车| 青河县| 获嘉县| 宜宾市| 克东县| 巫溪县| 靖边县| 融水|