Answers.Body: <p>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:</p>
  
  <p><a href="https://i.sstatic.net/rexix.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/rexix.png" alt="enter image description here"></a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-09T21:49:23.823Z
Answers.LastActivityDate: 2015-10-09T21:49:23.823Z
Answers.UserDisplayName: Coleman
Answers.Body: <p>Two reasons I can think of:</p>
  
  <ul>
  <li>what happens if you upload twice a file with the same name for different contacts? the hash allow to avoid overwriting</li>
  <li>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 </li>
  </ul>
  
  <p>This being said, it's been there since forever and I never quite questioned it.</p>
  
  <p>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</p>
  
  <p>as andy detailled, you need to touch a few more tables if you want to upload the file in a custom field</p>
  
  <ul>
  <li>add a row to civicrm_file with the file name</li>
  <li>add a row to civicrm_entity_file to link to the contact id, civicrm_file table &amp; your custom table </li>
  <li>update your custom table with the numeric id from the civicrm_file </li>
  <li>upload the file into the right folder</li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-15T14:47:12.963Z
Answers.Text: 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!
Answers.UserDisplayName: Andy Clark
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-16T06:55:23.833Z
Answers.Text: ah, i replied about the contact thumbnail, but indeed, slightly more complicated if custom field. i'm updating my answer with your extra info
Answers.UserDisplayName: Xavier
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-15T14:01:22.180Z
Answers.LastActivityDate: 2015-09-16T06:57:11.937Z
Answers.LastEditDate: 2015-09-16T06:57:11.937Z
Answers.UserDisplayName: Xavier
Body: <p>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 &amp; 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)</p>
  
Tags: <4.6.8>
Title: What's the purpose of the hash added to filenames in custom folder?
ThreadText: # What's the purpose of the hash added to filenames in custom folder?
  
  (Asked by: username_0 on 2015-09-15)
  
  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)
  
  **Tags:** 4.6.8
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2015-09-15. Score: 3 | Accepted answer
  
  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 username_0 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
  
  > **Comments:**
  > **username_0**: Thanks, username_2, 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!  
  > **username_2**: ah, i replied about the contact thumbnail, but indeed, slightly more complicated if custom field. i'm updating my answer with your extra info  
  
  ---
  
  ### Answer by username_1 on 2015-10-09. Score: 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](https://i.sstatic.net/rexix.png)](https://i.sstatic.net/rexix.png)
  
  
  ---
  
  