anolan13 Posted December 4, 2008 Share Posted December 4, 2008 Hello, I need a way where if users upload a file to my server that's the same as another file (both .mp3 files) but don't necessarily have the same filename, the server will know and perform an action based on this. Is there a way for PHP to check certain characteristics in an mp3 file that would make it different from another file or the same as another file regardless of the file name? Just a quick example, These two files are the same but have different file names... mysong1.mp3 thebestsongever.mp3 Is there a way for the server to tell the difference or similarity once their both uploaded? Thanks, Link to comment https://forums.phpfreaks.com/topic/135531-way-to-tell-if-an-uploaded-file-matches-file-on-server-without-same-file-name/ Share on other sites More sharing options...
Mchl Posted December 4, 2008 Share Posted December 4, 2008 MD5() both files and compare Link to comment https://forums.phpfreaks.com/topic/135531-way-to-tell-if-an-uploaded-file-matches-file-on-server-without-same-file-name/#findComment-706038 Share on other sites More sharing options...
gevans Posted December 4, 2008 Share Posted December 4, 2008 Mchl, can you jsut md5 the dir/filename to the file? (didn't know this) Link to comment https://forums.phpfreaks.com/topic/135531-way-to-tell-if-an-uploaded-file-matches-file-on-server-without-same-file-name/#findComment-706050 Share on other sites More sharing options...
Mchl Posted December 4, 2008 Share Posted December 4, 2008 That's what md5() is for: http://www.php.net/downloads.php Link to comment https://forums.phpfreaks.com/topic/135531-way-to-tell-if-an-uploaded-file-matches-file-on-server-without-same-file-name/#findComment-706056 Share on other sites More sharing options...
gevans Posted December 4, 2008 Share Posted December 4, 2008 I don't know how I completely forgot about that. So it will match if both .mp3 files have the same content? Link to comment https://forums.phpfreaks.com/topic/135531-way-to-tell-if-an-uploaded-file-matches-file-on-server-without-same-file-name/#findComment-706061 Share on other sites More sharing options...
gevans Posted December 4, 2008 Share Posted December 4, 2008 Wouldn't you have to use md5_file() ?? Link to comment https://forums.phpfreaks.com/topic/135531-way-to-tell-if-an-uploaded-file-matches-file-on-server-without-same-file-name/#findComment-706064 Share on other sites More sharing options...
Mchl Posted December 4, 2008 Share Posted December 4, 2008 No. It will match if both files generate same md5(). There is very low probability, that two different files will generate the same md5() (situation called collision). To reduce risk of collisions, one may use hash with some other hashing algorithm, but I think in this case md5() should be sufficient. The best would be to calculate md5 whenever a file is uploaded and compare it against hashes of previously uploaded files (preferably stored in database). Wouldn't you have to use md5_file() ?? That would be actually easier than loading a file into string There's also hash_file Link to comment https://forums.phpfreaks.com/topic/135531-way-to-tell-if-an-uploaded-file-matches-file-on-server-without-same-file-name/#findComment-706069 Share on other sites More sharing options...
gevans Posted December 4, 2008 Share Posted December 4, 2008 Sounds good, nice to remember. Link to comment https://forums.phpfreaks.com/topic/135531-way-to-tell-if-an-uploaded-file-matches-file-on-server-without-same-file-name/#findComment-706074 Share on other sites More sharing options...
Mchl Posted December 4, 2008 Share Posted December 4, 2008 Here's a nice site about MD5 usage for identifying files http://www.win.tue.nl/hashclash/Nostradamus/ Link to comment https://forums.phpfreaks.com/topic/135531-way-to-tell-if-an-uploaded-file-matches-file-on-server-without-same-file-name/#findComment-706078 Share on other sites More sharing options...
gevans Posted December 4, 2008 Share Posted December 4, 2008 Very interesting, and nice use of a ps3 Link to comment https://forums.phpfreaks.com/topic/135531-way-to-tell-if-an-uploaded-file-matches-file-on-server-without-same-file-name/#findComment-706080 Share on other sites More sharing options...
PFMaBiSmAd Posted December 4, 2008 Share Posted December 4, 2008 I would compare file sizes and the md5/checksum. Files that are different sizes cannot be the same. This will further reduce the chance of different files having the same md5/checksum. Link to comment https://forums.phpfreaks.com/topic/135531-way-to-tell-if-an-uploaded-file-matches-file-on-server-without-same-file-name/#findComment-706244 Share on other sites More sharing options...
Mchl Posted December 4, 2008 Share Posted December 4, 2008 True dat! Compare sizes first, so you can save some CPU cycles calculating hash when they're different Link to comment https://forums.phpfreaks.com/topic/135531-way-to-tell-if-an-uploaded-file-matches-file-on-server-without-same-file-name/#findComment-706254 Share on other sites More sharing options...
PFMaBiSmAd Posted December 4, 2008 Share Posted December 4, 2008 Actually, the third step if you want to know for certain that files are not the same, if you have matching lengths and md5/checksums, would be to do a byte-by-byte comparison. Link to comment https://forums.phpfreaks.com/topic/135531-way-to-tell-if-an-uploaded-file-matches-file-on-server-without-same-file-name/#findComment-706306 Share on other sites More sharing options...
Mchl Posted December 5, 2008 Share Posted December 5, 2008 I think I'd just compare hashes generated by some other function... Probability that there's collision of md5 hashes is low already, and probability that both md5 and sha1 (or something) hashes are the same is like... nil[1]. And I can hardly see anyone manipulating a file so that he can not upload it [1] nil is not zero, but close enough Link to comment https://forums.phpfreaks.com/topic/135531-way-to-tell-if-an-uploaded-file-matches-file-on-server-without-same-file-name/#findComment-706587 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.