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
Author by
A Clockwork Orange
Updated on June 12, 2022Comments
-
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 about 13 yearsYes. MD5 is fine. It's just for debugging right now.
-
gideon about 13 yearsthink 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 about 13 yearsSee the second answer here. : stackoverflow.com/questions/770900/…
-
A Clockwork Orange about 13 yearsThis 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 about 13 yearsJust 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 about 13 years65Fbef05, Thank you but I understand how hashing functions work :]
-
-
nerkn about 13 yearsI'm late :) +1 for exactly same answer