1

I tried many things like base 64 or just using the php function file_get_contents but when i check the civicrm UI and click on the attachment it doesn't work (for images for example it says unrecognized format). So what is the proper way to use the content property in the create action in the API V3 enter image description here

3
  • While you might be able to get it to work, I'm not sure that's what the content property was designed for. I think it was just for text. You probably need to store it in a file and then instead of content use path.
    – Demerit
    Commented May 1, 2023 at 16:18
  • @Demerit the path property is read only, i don't think it's possible to do so if you check the database table civicrm_file you find no path column, instead you find the uri which is just the file name, then civicrm will do the rest and calculate the path depending only on the file name, at least that's what i understood. Commented May 2, 2023 at 11:13
  • Ah you're right. Take a look at the unit test here: github.com/civicrm/civicrm-core/blob/…. The trick seems to be to use 'options' => ['move-file' ...], which gets used in the api call here: github.com/civicrm/civicrm-core/blob/…
    – Demerit
    Commented May 2, 2023 at 13:48

1 Answer 1

0

There's a unit test at https://github.com/civicrm/civicrm-core/blob/3a822c76c86397199c53f750e71081d548f45a96/tests/phpunit/api/v3/AttachmentTest.php#L93-L100 and https://github.com/civicrm/civicrm-core/blob/3a822c76c86397199c53f750e71081d548f45a96/tests/phpunit/api/v3/AttachmentTest.php#L316-L319 that together describe how to use this. Putting them together it would be:

civicrm_api3('Attachment', 'create', [
  'entity_table' => 'civicrm_activity',
  'entity_id' => $activityId,
  'name' => 'example.png',
  'mime_type' => 'image/png',
  'description' => 'My test description',
  'options' => [
    'move-file' => 'temporary/path/to/example.png',
  ],
]);

Your Answer

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

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