Jump to content

Recommended Posts

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.";
}

Link to comment
https://forums.phpfreaks.com/topic/189608-md5_file/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/189608-md5_file/#findComment-1000739
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/189608-md5_file/#findComment-1000740
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.