3

I have a ton of image files to add to Civi (far too many to do manually) and have figured out what tables to update & make it all work. But I notice that files uploaded via the Civi UI have a hash string added to the filename that's stored. So xyz.pdf becomes xyz$$$.pdf where $$$ is the hash. What's the purpose of this hash string? Is it to make a unique URI? Should I do this as well for my files? It all works without this. (I don't want to use the API by the way, and we're on 4.6.8)

3

Two reasons I can think of:

  • what happens if you upload twice a file with the same name for different contacts? the hash allow to avoid overwriting
  • probably increase a bit the protection and privacy too, eg. by guessing and trying to download warren_buffet.jpg or bill_gates.jpg, we could know if you have some wealthy donors in your db

This being said, it's been there since forever and I never quite questioned it.

If it's for mass uploading contact images, you can probably just set the field into civicrm_contact and put the image at the right place, I remember having done something like that a while ago, it probably still works

as andy detailled, you need to touch a few more tables if you want to upload the file in a custom field

  • add a row to civicrm_file with the file name
  • add a row to civicrm_entity_file to link to the contact id, civicrm_file table & your custom table
  • update your custom table with the numeric id from the civicrm_file
  • upload the file into the right folder
| improve this answer | |
  • 1
    Thanks, Xavier, I will add a unique hash key. For others to know, you have to 1) add a row to civicrm_file with the file name 2) add a row to civicrm_entity_file to link to the contact id, civicrm_file table & your custom table 3) update your custom table with the numeric id from the civicrm_file 4) upload the file. QED! – Andy Clark Sep 15 '15 at 14:47
  • ah, i replied about the contact thumbnail, but indeed, slightly more complicated if custom field. i'm updating my answer with your extra info – Xavier Sep 16 '15 at 6:55
0

If you are spending time trying to manually reproduce how Civi handles files in your own custom code, consider using the Attachment api, which will do it all for you:

enter image description here

| improve this answer | |

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.