开发者

Preventing duplicate image uploads via PHP

开发者 https://www.devze.com 2023-04-11 06:44 出处:网络
I have written a script to upload an image to a particular portion of my site. What kind of check do I need to do to detect if a duplicate entry is trying to be uploaded through the form?

I have written a script to upload an image to a particular portion of my site.

What kind of check do I need to do to detect if a duplicate entry is trying to be uploaded through the form?

Example: One user submits firefoxlogo.jpg. Another user a few days later tries su开发者_开发百科bmitting firefox.jpg which is the same image, just renamed.


...the same image...

The same as "the binary data is identical" or "the image looks similar"? In the first case, you can calculate the hash of a file using sha1_file (for SHA1 hashes). You should never rely on the filename for checking whether files are unique or not. E.g. one user could upload "firefox.png" containing the browser's logo and someone else a screenshot of it. The hash has a fixed length (40 for SHA1) which is another advantage over using filenames.


Each time a user uploads a file you could keep a record of it's sha1 hash (using sha1_file) in your database. When you get a file upload, grab the hash of the new file while it's still in temporary storage, then query your database for an entry with the same hash. If none exists, you can continue to upload the file.


see this too http://php.net/exif , not totally secure to avoid duplicates, but a faster solution to sha1_file

hope this helps

0

精彩评论

暂无评论...
验证码 换一张
取 消