10

I would like to hash the contents of an uploaded file in MD5. The file will not be saved locally so it only exists in a tmp directory.

How can I do this? Thanks.

A Clockwork Orange
  • 23,913
  • 7
  • 25
  • 28
  • Yes. MD5 is fine. It's just for debugging right now. – A Clockwork Orange Apr 06 '11 at 19:43
  • think there is `md5_file()`? See this [question](http://stackoverflow.com/questions/2304081/php-get-the-md5-of-remote-file) Also you may want to use a stronger hash like SHA that will have less possibilities of collisions. – gideon Apr 06 '11 at 19:47
  • See the second answer here. : http://stackoverflow.com/questions/770900/is-md5-less-secure-than-sha-et-al-in-a-practical-sense – gideon Apr 06 '11 at 19:48
  • This is just for debugging. Only up to 3 files will be stored so I don't need to worry about collisions. I different hashing function will be used in the final, I just need to make sure the rest of it works. – A Clockwork Orange Apr 06 '11 at 19:50
  • Just as a note, hashing a file in md5 does not provide storage ability - its use is more for file verification. You cannot 'decode' the md5 hash to create a usable file. – 65Fbef05 Apr 06 '11 at 19:51
  • 65Fbef05, Thank you but I understand how hashing functions work :] – A Clockwork Orange Apr 06 '11 at 20:22

3 Answers3

21

You can use md5_file() or sha1_file() function. For example, if your post variable is filevar:

$myhash = md5_file($_FILES['filevar']['tmp_name']);
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Charles Brunet
  • 21,797
  • 24
  • 83
  • 124
3

You can use md5_file(), even on your temporary file.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
2

MD5() for string and md5_file() for files

Thoman
  • 742
  • 2
  • 9
  • 20