views:

27

answers:

2

I've been trying to hash the contents of some zip files from a remote source using PHP's md5_file function:

md5_file($url);

I'm having a problem with a couple of URLs; I'm getting the following error:

Warning: md5_file($url): failed to open stream: HTTP request failed!

I think it's because the zip files are quite large in those cases.

But as yet I haven't been able to find much information or case studies for md5_file hashing remote files to confirm or refute my theory. It seems most people grab the files and hash them locally (which I can do if necessary).

So I suppose it's really out of curiosity: Does md5_file have any specific limits to how large remote files can be? Does it have a timeout which will stop it from downloading larger files?

A: 

Probably the simplest solution is to set the timeout yourself via:

ini_set('default_socket_timeout', 60);  // 60 secs

Of course if these files are big, another option is to use file_get_contents() as you can specify the filesize limit. You don't want to assign this to a value, as it's more efficient to wrap it like so:

$limit = 64 * 1024; // 64 being the number of KB to limit your retrieval
md5(file_get_contents($url, false, null, 0, $limit )); 

Now you can create MD5s off parts of the file, and not worry if somebody tries to send you a 2GB file. Of course keep in mind it's only a MD5 for part of the file, if anything after that changes, this breaks. You don't have to set a filesize limit at all, just try it like so:

ini_set('default_socket_timeout', 60);  // 60 secs
md5(file_get_contents($url));
TravisO
`md5` does only have two parameters.
Gumbo
@Gumbo fixed, it was a typo
TravisO
A: 

Some hosting environments don't allow you to access remote files in this manner. I think that the MD5 function would operate a lot like the file() function would. Make sure you can access the contents of remote files with that command first. If not, you may be able to CURL your way to the file and it's contents.

Evan