php检测远端文件是否存在的方法

WBOY
풀어 주다: 2016-07-25 09:07:28
원래의
1205명이 탐색했습니다.
  1. function get_http_response_code($theURL) {
  2. $headers = get_headers($theURL);
  3. return substr($headers[0], 9, 3);
  4. }
  5. ?>
复制代码

get_headers的作用就是访问一个远程地址,把服务器发送的HTTP头以数组形式返回。而$header[0]则是服务器返回的状态码(如果不出意外的话状态码应该都是第一个返回的)。

排除重定向的例子:

  1. /**

  2. * Fetches all the real headers sent by the server in response to a HTTP request without redirects
  3. * 获取不包含重定向的报头
  4. */
  5. function get_real_headers($url,$format=0,$follow_redirect=0) {
  6. if (!$follow_redirect) {
  7. //set new default options
  8. $opts = array('http' =>
  9. array('max_redirects'=>1,'ignore_errors'=>1)
  10. );
  11. stream_context_get_default($opts);
  12. }
  13. //get headers

  14. $headers=get_headers($url,$format);
  15. //restore default options
  16. if (isset($opts)) {
  17. $opts = array('http' =>
  18. array('max_redirects'=>20,'ignore_errors'=>0)
  19. );
  20. stream_context_get_default($opts);

  21. }
  22. //return

  23. return $headers;
  24. }
  25. ?>
复制代码


원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!