dreamwest Posted January 24, 2010 Share Posted January 24, 2010 Im trying to check if a file is a duplicate based on the md5 of the file, I have this so far but im not sure which way to go with it $file = $_FILES["file"]["name"]; $md_file = md5_file($file); $dir = getcwd(); if(file_exists($dir.$md_file){ echo "Image already exists. Please upload a different image."; } Quote Link to comment https://forums.phpfreaks.com/topic/189608-md5_file/ Share on other sites More sharing options...
jl5501 Posted January 24, 2010 Share Posted January 24, 2010 It is not clear why you are using md5() here. The file_exists() function will check its parameter against the actual file system name, so unless you are saving files as their md5()'d name, then this will not do anything for you. To check for a duplicate file name, you would simply use the actual name of the file. Quote Link to comment https://forums.phpfreaks.com/topic/189608-md5_file/#findComment-1000739 Share on other sites More sharing options...
Daniel0 Posted January 24, 2010 Share Posted January 24, 2010 You would want to use md5_file() on $_FILES['file']['tmp_name'] instead. It needs a path to a file on the filesystem and it returns the MD5 hash of its contents. Either you can store MD5 hashes for files in a database and do a query to check if it already exists, or you can iterate through all the files in your upload directory, get their MD5 hash and compare it to the new one. To check for a duplicate file name, you would simply use the actual name of the file. A file can still be a duplicate even though it has a different name. On your computer, try to make file and then copy-paste it in the same folder with a different name. Do we agree it's a duplicate? One heuristic for checking whether a file is a duplicate or not can be comparing hashes of their contents, in this case using MD5. Quote Link to comment https://forums.phpfreaks.com/topic/189608-md5_file/#findComment-1000740 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.