开发者

Using PHP to determine if a remote file has been replaced?

I have a MySQL database with some URLs in it. One URL per row. Each URL has my script on it. What I am wanting to do, is ch开发者_StackOverflow中文版eck if the file is still there via a PHP script. Not check if it 404'd, but rather check if it has been modified or replaced. Is this possible? If so, how would it be accomplished?

I was thinking making the remote file echo some string, and having the local file check the page for that string, but that seems a little inefficient and sloppy.

EDIT: The remote files don't output any data to the remote users, they trigger internal scripts, so remote users would only see a white page. (The checking script would only see this as well)


Bulletproof Way

  1. Fetch the URL contents.
  2. Compute the MD5() or SHA1() hash.
  3. Fetch the URL again and check if the hash has changed.

There are other ways (such as the Last-Modified HTTP header), but they don't work with all servers.


Well you can ask to the file to send his md5 under certain conditions, for example:

if($_GET['md5hash']==true)
{ 
  echo md5($_SERVER['SCRIPT_FILENAME']);
}

You can easily understand if your script is changed by receiving his md5 or receiving something different by his md5, but i don't know what is the score for you.


just use

md5_file('http://path.to/file');

and store the hash for checking later


if the urls are local files you can use filemtime on each file and check the time against the modifiedtime row in the table.


You may also be able to use stat on a remote file and check the mtime record.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜