您的位置:首页 > 编程语言 > PHP开发

PHP获取远程文件最后修改时间

2013-12-30 12:49 453 查看
这个函数是用来得到远程文件的最后修改时间,前提是服务器header返回last modified,否则无效。

function remote_filectime($url_file){
$headInf = get_headers($url_file,1);
return strtotime($headInf['Last-Modified']);
}


转载:http://www.pocketdigi.com/20101021/124.html

get_headers[1]()
返回一个数组,包含有服务器响应一个 HTTP 请求所发送的标头。如果失败则返回 FALSE 并发出一条 E_WARNING 级别的错误信息(可用来判断远程文件是否存在)。

如果将可选的 format 参数设为 1,则 get_headers() 会解析相应的信息并设定数组的键名。


参数

url

目标 URL。

format

如果将可选的 format 参数设为 1,则 get_headers() 会解析相应的信息并设定数组的键名。

例子

<?php

$url = '';//远程地址,需要添加http://

  print_r(get_headers($url));

  print_r(get_headers($url, 1));

  ?>

上例的输出类似于:

Array

(

[0] => HTTP/1.1 200 OK

[1] => Date: Sat, 29 May 2004 12:28:13 GMT

[2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)

[3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT

[4] => ETag: “3f80f-1b6-3e1cb03b”

[5] => Accept-Ranges: bytes

[6] => Content-Length: 438

[7] => Connection: close

[8] => Content-Type: text/html

)

Array

(

[0] => HTTP/1.1 200 OK

[Date] => Sat, 29 May 2004 12:28:14 GMT

[Server] => Apache/1.3.27 (Unix) (Red-Hat/Linux)

[Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT

[ETag] => “3f80f-1b6-3e1cb03b”

[Accept-Ranges] => bytes

[Content-Length] => 438

[Connection] => close

[Content-Type] => text/html

)
http://baike.baidu.com/view/10172365.htm
用PHP的朋友,大家都知道is_file和file_exists都可以判断文件是否存在或者是目录是否存在;关于这2个函数的功能及其效率网上也有比较好的分析。这里就不多说了。

那如何判断一个远程URL地址是否真实有效呢。比如我们在批量采集了一批数据导入到自己的库中。可能过了段时间别人的数据更新了。也许某个采集的图片地址就失效了。如果我们没能及时发现。用户看到的就是一个X。对用户体验来说是很不友好的。这时我们要通过一个方法在用户点开页面的时候检查一下图片是否真实存在,如果不存在就应该使用服务上的一张默认图片取代这张图片的URL。然后我们可以将这个图片所在的文章或产品之类的ID记录下来。管理员只要通过查看这此记录就知道哪些图片已经失效。从而可以使用新的图片更新上去。

下面是我们说下怎么判断URL的真实性呢。我推荐大家使用get_headers函数。下面是get_headers的定义:

get_headers

(PHP 5)
get_headers -- Fetches all the headers sent by the server in response to a HTTP request

Description

array get_headers ( string url [, int format] )

get_headers() returns an array with the headers sent by the server in response to a HTTP request. Returns FALSE on failure and an error of level E_WARNING will
be issued.

If the optional format parameter is set to 1, get_headers() parses the response and sets the array's keys.

简单的说就是返回一个HTTP请求的头文件信息,格式如下:

【1】

Array ( [0] => HTTP/1.0 200 OK [1] => Server: nginx [2] => Date: Tue, 11 Dec 2012 02:55:22 GMT [3] => Content-Type: image/jpeg [4] => Content-Length: 33595 [5] => Last-Modified: Fri, 09 Dec 2011 03:11:08 GMT [6]
=> Accept-Ranges: bytes [7] => Expires: Thu, 31 Dec 2037 23:55:55 GMT [8] => Cache-Control: max-age=315360000 [9] => X-Cache: MISS [10] => X-Cache: MISS from CT-ZJWZ-203-188.fastcdn.com [11] => Age: 82 [12] => X-Cache: HIT from CT-SHCB-169-11.fastcdn.com [13]
=> Connection: close )
【2】

Array ( [0] => HTTP/1.0 404 Not Found [1] => Server: nginx [2] => Date: Tue, 11 Dec 2012 03:06:19 GMT [3] => Content-Type: text/html [4] => Content-Length: 162 [5] => X-Cache: MISS from CT-ZJWZ-203-188.fastcdn.com [6] => X-Cache: MISS from CT-SHCB-169-11.fastcdn.com [7] => Connection: close )


我们可以使用里面的这些参数做很多优化。现在我就说与本文章有关的优化。就是使用页面的状态码。例如上面的1就是一个URL有效的返回。键名0所指向的键值很明显有个200,例2所显示的是404.我相信大家都懂的。接下来的我就不用多说了。大家都懂的。自由发挥吧。

转载;http://blog.sina.com.cn/s/blog_78ecbe3301013ym1.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: