Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

API Upload attachments with PHP + cURL

Ricardo Pagano
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 3, 2021

Hi! i'm developing an page who is capable to create a new card using some input/select, and i'm trying to create an input type="file" multiple.

Everything is working except upload, i'm using an ajax call using javascript to send the file to one PHP called ajaxResponse.php who is responsible to create a new card and upload the attachments, my doubt is:

- The multiple files is working fine and creating the attachments in the card ie already created, however, the files in trello when the ajax call is done, instead of showing me the right file with the correct extension, the attachment always have 19 bytes, and create an filename which name is Upload, if i download this file and check the content with one text editor, the content of this file is the filename i uploaded in HTML, can anybody tell me how was the correctly way to host a file using API + PHP+ cURL, please?

my Codes:

 

JAVASCRIPT

var files = document.getElementById('anexoTrello').files;

var formData = new FormData();

for(i = 0; i < myFiles.length; i++){

  formData.append('file[]', myFiles[i]);

}

var ajax = new XMLHttpRequest();

ajax.open('POST', 'assets/php/ajaxResponse.php');

ajax.send(formData);

 

PHP

 

if(!empty($_FILES)){
  for ($i=0; $i < count($_FILES['file']['name']); $i++) {
    $chAttachment = curl_init();
    curl_setopt($chAttachment, CURLOPT_URL, 'https://api.trello.com/1/cards/tQxhxRJO/attachments?key=key&token=token');
    curl_setopt($chAttachment, CURLOPT_POSTFIELDS, http_build_query(
      array(
        'name' => $_FILES['file']['name'][$i],
        'file' => $_FILES['file']['tmp_name'][$i]
      )
    ));

    curl_setopt($chAttachment, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($chAttachment);

    curl_close($chAttachment); print_r($result);

  }
}

 

Thanks!

1 answer

0 votes
Lara
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 8, 2021

Hi Ricardo,

Thanks for reaching out!

This would be a really great question to share in our developer community. We have a number of Trello Engineers that are active there, and they'd be happy to give some guidance for more advanced topics like these.

https://community.developer.atlassian.com/c/trello

Also, if it helps, there have been some changes made to how users can access attachments via the API, discussed further here: https://trello.com/c/tnX5dhiI/87-updated-authenticated-access-to-s3

Please let us know if you have any other questions, and we'll be happy to help.

All the best,

Lara
The Trello Team

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events