3

I see an older thread on the forum about potential improvements to the API to allow file uploads, but I'm not sure that ever happened. Has anyone ever uploaded files in CiviCRM using the API?

I see in the API Explorer that there is a File > Create with a parameter for File Contents but I'm not sure if you feed in the MIME contents, a URL, or...?

File via API

2
  • fantastic, that is good to know. does anyone know if we can provide an externalized attachment storage, example s3 buckets or firebase storage ? That is easier to scale than having it on the server running civi. Apr 6, 2019 at 14:32
  • I think that warrants a question of its own. Also I don't know the answer. :)
    – Laryn
    Jun 11, 2019 at 19:50

2 Answers 2

5

Yes, it did happen and it's called the "attachment" api. You can look up the docs for it in the api explorer:

enter image description here

2
  • Thanks! And I confess my ignorance - does webform-civicrm use this so file uploads don't rely on the form submission not being deleted anymore?
    – Laryn
    May 30, 2016 at 14:47
  • 2
    It does for almost everything. The one exception is the "contact image_url" because Civi does not store an actual file in that field, just the url. So that currently is still only stored in the webform submission.
    – Coleman
    May 31, 2016 at 3:39
0

Example to upload a image in custom field. Important, if you want to upload in custom field, you can't fill the entity table.

The field need to be of type file.

$fields = [
  'key' =>  $key,
  'api_key' => $api_key,
  'entity' => 'Attachment',
  'action' => 'create',
  'mime_type' => 'image/jpeg',
  'name' => 'test.jpg',
  'content' => file_get_contents('test.jpg'),
  'entity_id' => 2, // Contact id, contribution_id,....
  'field_name' => 'custom_247' // Custom field type file
];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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