I have a content type, Image, which allows users to add an image using Drupal 7's built-in image field. I would like to automatically prevent users from adding the exact same image (same filename, same size) if possible. Is there any way to prevent duplicate file uploads?


In pseudo/quick code, this is how I would approach it. In a custom module...

  1. Create a table in the db, with two columns, fid and md5.

  2. Each time you create a file, check if the md5() of whatever you consider unique is in that db, eg:

    a. select fid from TABLE where md5 = ab3783ea6c83bcd9383eef778

    b. if fid then oops, there already appears to be file blah blah blah

    c. otherwise

  3. Store that file's fid and md5() in that table.

You could put all this logic in the validation hook when you are creating the image content. You could even set up roles that are able to override it, etc.

Now, to the md5()ing of things.

  1. Filename is easily changed and I don't think it is really sent to drupal anyway--but instead crafted there after the fact--so do you really want to use it?

  2. Content of the file might be a better approach.

So, md5 could be populated with a quick md5_file(PATHTOFILE).

Now, let's say you wanted to have an option of doing this system wide or per user, eg, you don't want one user uploading multiple copies of one image or you want to have only one exact image uploaded systemwide.

Just add another column uid to the table above and check it as well, eg:

select fid from TABLE where md5 = ... and uid = $user->uid

Anyway, these are just my quick thoughts after the morning's first espresso here in D6land. Maybe D7 has some of this already built into it and I am once again reinventing that wheel :)

| improve this answer | |
  • This is essentially the approach I was about to write up. If you make the MD5 the PK of the table, the logic becomes a bit simpler because you can't INSERT duplicates. – mpdonadio Jul 20 '12 at 14:03
  • I agree on the PK if you don't want to also allow/do it by uid. – Jimajamma Jul 20 '12 at 14:59
  • @MPD I would make fid from file_managed PK here, for consistency. Schema supports 'unique keys'. – Mołot Jun 14 '13 at 8:27

There is now a module for this, the File Hash module. Many thanks to Jimajamma for outlining his approach, which allowed me to find this module via Google.

As shown in this thread, the module can currently be used to disable duplicate file uploads globally.

| improve this answer | |
  • How to achieve this in D6 ? any idea !!! – Bala Jun 15 '13 at 7:46
  • See Jimajamma's answer. A new module will need to be written (or backport the File Hash module). – Patrick Kenny Jun 15 '13 at 8:10
  • backport means ??? – Bala Jun 15 '13 at 8:11
  • Port the File Hash module from Drupal 7 back to Drupal 6. Drupal modules are specific to the major release they target (6, 7, 8, etc.), so to use a Drupal 7 module on Drupal 6, a conversion will have to be performed. How difficult this is depends on the complexity of the module. A good place to start is to open a feature request in the module's issue queue for a D6 backport and see what the maintainer says. – Patrick Kenny Jun 15 '13 at 8:14

I think you need to write a custom module to achieve this. I have done this before in bits and pieces. Use a form alter and see for the fields. In that get the name and the size of the file, sure some variable would have got these two.

  1. Try if you can get these values from Database using db_query.
  2. If not save them dynamically using the "static $variable_name = $field['name']; and similarly for size also.

Now try to use a if() and see if(!isset($field['name']) && !isset($field['size'])). And based on this set the error message in the form.

Hope it helps you.

| improve this answer | |
  • Good approach, but you could have shared your code! – Gladiator Jul 20 '12 at 13:01
  • I said that i did this in bits and pieces, i am not sure about the exact way to implement this:( – user4081 Jul 20 '12 at 13:02
  • 1
    But I doubt whether the db_query can be used here or not. But the second approach to use static will lead to inconsistency. I will not consider that as a long term solutions. Can you give some clarity. – Gladiator Jul 20 '12 at 13:03
  • 1
    maybe also look into node_presave hook, – Vic Jul 20 '12 at 13:27
  • 3
    If you use presave, the only thing you can do at that point is unset the field if it's not unique and let the user know about it after the node has been saved, if it can even be saved sanely at all without this field. regardless of approach, this logic should be in the form validation so the user has a chance to add a different image or not save the node in the first place. – Jimajamma Jul 20 '12 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.