-2

I have a site where users upload photos, but it's important that they don't upload the same photo twice. I need to compare each uploaded file to all of the photos in a directory to see if the image already exists. The one catch- I can't use the file name. Is there a way to compare the sizes of all of the photos at once to see if there is a match?

I haven't tried any code yet as I have no idea if this is possible.

Derek Brown
  • 4,232
  • 4
  • 27
  • 44
Mekade24
  • 25
  • 5

2 Answers2

3

What you are looking for is a hash function, it takes as input a file and returns a (much smaller) value which is unique for the file. When a new file is uploaded, you then compare the new hash value to the database of previous hash values to determine if it is a new file or not.

I would look into using the PHP hash function, you would input the uploaded file (which I assume is in a variable), and get a hash value back. If you actually are saving files to disk prior to computing the hash value, sha1_file is what you are looking for.

Derek Brown
  • 4,232
  • 4
  • 27
  • 44
  • Thank you, I believe this is what I am looking for. Would I need to create a completely separate database to store the hash files, or could I change the file name itself to the hash value during the initial upload? Also, is there a guarantee that every hash file will be unique unless the file is identical? @Derek Brown – Mekade24 May 14 '18 at 18:01
  • 1
    @Mekade24 you could absolutely change the filenames to be the hash name- that would be a super easy solution. – Derek Brown May 14 '18 at 18:06
  • @Mekade24 two files have unique hash values with very very very high probability (see https://stackoverflow.com/questions/1867191/probability-of-sha1-collisions). – Derek Brown May 14 '18 at 18:09
-2

Use the following:-

<?php
$directoryName = 'imagesDir';
$fileToBeChecked = 'newlyUploadedImageFilePath';

function checkIfSameSizeFileExistsInDirectory($dir, $file){
    $entries = array_diff(scandir($dir), array('.', '..'));
    foreach ($entries as $entry){
        if(!is_dir($entry)){
            if(filesize($entry)==filesize($file)){
                return true;
            }
        }
    }
    return false;
}
?>
nandal
  • 2,544
  • 1
  • 18
  • 23
  • 1
    Just because two files are the same size doesn't mean that they are the same file. This would not work. – Derek Brown May 14 '18 at 18:06
  • I can understand that, however the user asked for comparing the file exists with same size. – nandal May 14 '18 at 18:09
  • However, in order to find the same file from directory, Hash comparison is the obvious and efficient way. But user can use the above code to size comparison. The above solution can assist in that. – nandal May 14 '18 at 18:10