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
<span>var</span> files = document.getElementById(<span>'anexoTrello'</span>).files;
<span>var</span> formData = <span>new</span> FormData();
<span>for</span>(i = <span>0</span>; i < myFiles.length; i++){
formData.append(<span>'file[]'</span>, myFiles[i]);
}
<span>var</span> ajax = <span>new</span> XMLHttpRequest();
ajax.open(<span>'POST'</span>, <span>'assets/php/ajaxResponse.php'</span>);
ajax.send(formData);
PHP
if(!empty($_FILES)){
for ($i=0; $i < count($_FILES['file']['name']); $i++) {
$chAttachment = curl_init();
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!