PHP - Hash contents of uploaded file

11,469

Solution 1

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

$myhash = md5_file($_FILES['filevar']['tmp_name']);

Solution 2

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

Solution 3

MD5() for string and md5_file() for files

Share:
11,469
A Clockwork Orange
Author by

A Clockwork Orange

Updated on June 12, 2022

Comments

  • A Clockwork Orange
    A Clockwork Orange about 2 years

    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
      A Clockwork Orange about 13 years
      Yes. MD5 is fine. It's just for debugging right now.
    • gideon
      gideon about 13 years
      think there is md5_file()? See this question Also you may want to use a stronger hash like SHA that will have less possibilities of collisions.
    • gideon
      gideon about 13 years
      See the second answer here. : stackoverflow.com/questions/770900/…
    • A Clockwork Orange
      A Clockwork Orange about 13 years
      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.
    • 65Fbef05
      65Fbef05 about 13 years
      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.
    • A Clockwork Orange
      A Clockwork Orange about 13 years
      65Fbef05, Thank you but I understand how hashing functions work :]
  • nerkn
    nerkn about 13 years
    I'm late :) +1 for exactly same answer