麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 語言 > 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.companysz.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. ?>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美成人午夜一区二区三区 | 成人444kkkk在线观看 | 91久久夜色精品国产网站 | 国产美女精品视频 | 亚洲啪| 成人羞羞视频在线观看免费 | 久久久av影视 | 中文字幕涩涩久久乱小说 | 爱性久久久久久久 | 欧美熟videos肥婆 | 女人裸体让男人桶全过程 | 国产最新网站 | 国产美女视频一区二区三区 | 91午夜视频 | 国内精品久久久久久久久久 | 中国免费一级毛片 | 一级一级一级一级毛片 | 欧美中文字幕一区二区 | 国产成人高清在线观看 | 国产精品一区在线观看 | 国产精品视频自拍 | 欧洲狠狠鲁 | 免费国产wwwwwww网站 | av成人免费看 | 天天草夜夜 | 成人免费入口 | 精品久久久久久综合日本 | 精品一区二区亚洲 | 欧美精品成人一区二区三区四区 | 国产妇女乱码一区二区三区 | 国产一国产一级毛片视频在线 | 干色视频| 免费在线观看午夜视频 | 中文字幕精品亚洲 | 欧美日韩在线看片 | av影院在线播放 | www.91视频com | 日韩中文字幕一区二区三区 | 国产精品一区二区三区在线 | 91九色免费视频 | 中文在线观看www |