Reputation: 193
I have PHP code that uploads multiple files at the same time. Currently I'm using name_timestamp pattern for the file name - in order to maintain uniqueness. Now the requirement is that suppose I uploaded Desert image twice (content remains the same) then there will be 2 entries with that timestamp. For example :
Desert_201409281000.jpg
Desert_201409282000.jpg
Is it possible to show some unique name like PHOTO1 only for this ?
I tried with JQuery for this but I want a PHP solution.
Upvotes: 4
Views: 689
Reputation: 775
How are you uploading the images?
If via external URL, then you could just check for the duplicate urls
If from the computer, then you could use some php library to check for duplicate images and then apply timestamp and a way to rename the files into a single Photo1.jpg file...
I believe you require the timestamp, if not, then plainly the first occurence of the image in the timestamped image list could be kept as a name for all the duplicates!
Also, after searching for a PHP solution to duplicate image files, I found this page: [ ::: link ::: ]
This page suggests that PHP has an inbuilt function called md5, that checks for the md5 values of any file. If you're storing the md5 filenames in SQL, then, in PHP, you can search for any match of the md5 values of the uploaded file in your SQL table, and rename the files as per your requirement.
Here's the code to verify for a md5 value from an MYSQL table:
$sign = md5(file_get_contents($filename)); //This will create an unique md5 signature for every picture
//You could now store that hash in your database and check for duplicates at every upload
$results = mysql_query("SELECT file FROM images WHERE signature=" . $sign);
if ($row = mysql_fetch_array($results)) {
//Duplicate found, $row['file'] contains name
}
As suggested, you could also use: $sign = md5_file($filename);
or sha1
Upvotes: 1