1

Similair to this question here: php md5_file function with gif file? but the answer doesn't cut it for my situation.

On certain images I apply a md5_file to, there are a few (out of 20000) that give 2 different results on a random basis.

Example code:

<?php
$moo = 'http://a1100.phobos.apple.com/us/r1000/069/Purple/v4/03/0f/cd/030fcd97-3958-5a96-f1ae-86e55382c529/mzl.lepmgpwz.350x350-75.jpg';
echo md5_file($moo);
?>

Results in either: 673ec8b2f87df0b41ebc17bba85edd9a OR: 235bf518387d59b13a7c820abe73d5eb

I tried to play around with md5(file_get_contents($moo)); and similair versions of sha1, and sha256, but the ones I tried all come up with 2 different results on a random basis.

My quick echo test notepad:

<?php
$moo = 'http://a1100.phobos.apple.com/us/r1000/069/Purple/v4/03/0f/cd/030fcd97-3958-5a96-f1ae-86e55382c529/mzl.lepmgpwz.350x350-75.jpg';
echo $moo.'<br /><br />';

echo '- '.md5(file_get_contents($moo)).'<br />';
echo '- '.md5_file($moo).'<br />';
echo '- '.md5($moo).'<br />';

echo '- '.sha1(file_get_contents($moo)).'<br />';
echo '- '.sha1_file($moo).'<br />';
echo '- '.sha1($moo).'<br/>';

?>

Is this common and is there any way to avoid this?

Right now I'm stuck with a few dead images since 1 function places md5_file('my_image'); in my dB with a different name than the resize function saved md5_file('my_image); as.

Community
  • 1
  • 1
Marcel
  • 1,279
  • 10
  • 12
  • 3
    you are probably pulling from different servers randomly. Apple has a few cache servers, and probably changes the modified date/maybe the compression on the image, resulting in a different hash... – Green Black Jan 23 '13 at 23:51
  • 3
    You can't rely on the md5sum of a file you're pulling from a remote source that you don't control. – glomad Jan 23 '13 at 23:54
  • @John right now I only counted 2 out of 15500 doing this though, it's just very odd. Cause even when loaded on the same time, `md5(file_get_contents($moo));` gives 673ec8b2f87df0b41ebc17bba85edd9a where `md5_file($moo);` gives 235bf518387d59b13a7c820abe73d5eb as a result. I seriously doubt anything changes to the image at that exact time for only these 2 I've been messing with for a week now. – Marcel Jan 23 '13 at 23:54
  • 3
    `673ec8b2f87df0b41ebc17bba85edd9a` is the hash of the actual image file. I believe `235bf518387d59b13a7c820abe73d5eb` is an error page or somesuch. Try saving the contents of this file (eg: file_put_contents) and you'll see exactly what it is. – NullUserException Jan 23 '13 at 23:56
  • @NullUserException guessing that's it then, since the image resize function always picks `673ec8b2f87df0b41ebc17bba85edd9a` and it's the function that also saves it. The function that checks if resized exists and places the URL in the dB picks 1 of the 2 randomly. So yeah, it makes sense in a way. Checked 15580 now, still 2 but still not acceptable. Guess I got some rewriting to do. Add answer! – Marcel Jan 24 '13 at 00:13
  • Ended up going for `md5(basename($moo))`. The saved file (`file_put_contents`) to resize will always have the same name as the URL used, since this URL is coming from a cache on my server I don't see how this can ever fail. Thanks for the comments. – Marcel Jan 24 '13 at 10:01

0 Answers0