Upload attachment with CURL

Hi everyone.

I am doing some API work right now and I am creating tasks and subtasks.
Now I need to add attachments from my form into the task.

$binary = file_get_contents($_FILES['file']['tmp_name'][$i]);
$fileName = $_FILES['file']['name'][$i];

$curl = curl_init();

        curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://app.asana.com/api/1.0/tasks/' . $taskGid . '/attachments',
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_POSTFIELDS => array('file' => $binary, 'resource_subtype' => 'asana', name => $fileName),
        CURLOPT_HTTPHEADER => array(
                    'Authorization: Bearer $token',
                    'Cookie: TooBusyRedirectCount=0; TooBusyRedirectCount=0;                                                                                          logged_out_uuid=54a01a0dd7857620640436eb9ab6e4e0' ),

$resp = curl_exec($curl);

This is what I have right now.

It does not upload the file at all as an attachment.
I have been looking through this forum, google and everything I could find, but I can’t find a solution that works here.


I successfully did it using nodeJS, and it was haaaaarrrddd. I remember seeing discussion on the forum, let me find it.

Did you check out



Yeah I did check those out. I have tried several things listed here and on stackoverflow, but it does not work.
I do not get any errors and the attachments does not get into Asana.

I am really happy to say it’s an interesting post to read. I learn new information from your article , you are doing a great job. Keep it up.

1 Like