# API php attachment

**URL:** <https://forum.asana.com/t/api-php-attachment/23715>\
**Category:** API & Developers\
**Tags:** api, attachments\
**Created:** [8 June 2018 14:29 UTC](https://forum.asana.com/t/api-php-attachment/23715 "2018-06-08T14:29:50Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon66349062](https://luna1.co/8d76cd.png) [@anon66349062](https://forum.asana.com/u/anon66349062)\
**Post date:** [8 June 2018 14:29 UTC](https://forum.asana.com/t/api-php-attachment/23715/1 "2018-06-08T14:29:51Z")

</div>

Hi there,

I tried to upload a file using Asana API in two ways:

- php-asana library (example file)
- making CURL request

and in both cases have received an error message: “file: File is not an object”.

Can you help me identify the problem?

Many Thanks!

---

<div class="post-metadata">

**Author:** ![Jeff\_Schneider](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/jeff_schneider/32/146_2.png) [@Jeff\_Schneider](https://forum.asana.com/u/Jeff_Schneider)\
**Post date:** [2 July 2018 18:51 UTC](https://forum.asana.com/t/api-php-attachment/23715/2 "2018-07-02T18:51:33Z")

</div>

Hey @anon66349062,

Thanks for reaching out. In order for us to reproduce and troubleshoot the issue, can you please share your code and the response you’re getting.

---

<div class="post-metadata">

**Author:** ![anon66349062](https://luna1.co/8d76cd.png) [@anon66349062](https://forum.asana.com/u/anon66349062)\
**Post date:** [3 July 2018 15:16 UTC](https://forum.asana.com/t/api-php-attachment/23715/3 "2018-07-03T15:16:50Z")

</div>

Hi Jeff,

here it is:

> <https://github.com/Asana/php-asana/blob/master/examples/example-create-task-and-upload.php>

It throws “file: File is not an object” on $client-\>attachments-\>createOnTask(…)

BTW I found the reason: this example doesn’t work with PHP 7.0+ because of CURL ( CURLOPT\_POSTFIELDS should not contain @ - see [PHP file transfer: Forget the @ - use `curl_file_create`](https://binfalse.de/2016/06/21/forget-the-at-use-curl-file-create/))

So, the next code works:

```
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://app.asana.com/api/1.0/tasks/' . $taskId . '/attachments');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $apiToken]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, ['file' => new \CURLFile($pathToFile, $fileType, $fileName)]);
$data = curl_exec($ch);

```

Thanks!

---

<div class="post-metadata">

**Author:** ![Kate\_Jordan](https://luna1.co/8d76cd.png) [@Kate\_Jordan](https://forum.asana.com/u/Kate_Jordan)\
**Post date:** [23 July 2019 05:05 UTC](https://forum.asana.com/t/api-php-attachment/23715/4 "2019-07-23T05:05:14Z")

</div>

I am also facing some dire difficulties in configuring the Asana API. While making CURL request it showing a huge list of errors.☹

---

<div class="post-metadata">

**Author:** ![Phil\_Seeman](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/phil_seeman/32/81525_2.png) [@Phil\_Seeman](https://forum.asana.com/u/Phil_Seeman)\
**Post date:** [23 July 2019 13:31 UTC](https://forum.asana.com/t/api-php-attachment/23715/5 "2019-07-23T13:31:04Z")

</div>

Hi @Kate_Jordan and welcome to the forum!

Are you having trouble specifically with uploading a file? That’s what this thread is about. If you’re having troubles with the API regarding anything other than a file upload, then please start a new thread here in the Developers & API section of the forum.

Also, please post the code of your CURL request and post the errors you’re getting in return. Those details will be needed in order to provide any further guidance. Thanks!

---

<div class="post-metadata">

**Author:** ![Jaffer](https://luna1.co/8d76cd.png) [@Jaffer](https://forum.asana.com/u/Jaffer)\
**Post date:** [12 August 2019 09:57 UTC](https://forum.asana.com/t/api-php-attachment/23715/6 "2019-08-12T09:57:36Z")

</div>

I tried to upload a pdf and doc attachments from the postman and the attachment uploaded successfully. I tried to validate the data in the uploaded attachment and it’s empty

---

<div class="post-metadata">

**Author:** ![joehanna](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/joehanna/32/21945_2.png) [@joehanna](https://forum.asana.com/u/joehanna)\
**Post date:** [6 February 2022 07:12 UTC](https://forum.asana.com/t/api-php-attachment/23715/7 "2022-02-06T07:12:44Z")

</div>

Thanks @anon66349062 - your PHP Curl snippet saved me from so much stress. I have been struggling a long time with this as I am avoiding using the Asana PHP Client.

---

<div class="post-metadata">

**Author:** ![Shane8](https://luna1.co/8d76cd.png) [@Shane8](https://forum.asana.com/u/Shane8)\
**Post date:** [26 July 2022 09:25 UTC](https://forum.asana.com/t/api-php-attachment/23715/8 "2022-07-26T09:25:04Z")

</div>

I also faced an issue of needing to **upload a file to a Task**. Here is my solution that worked for me.

```auto
<?php

require 'vendor/autoload.php';

$client = Asana\Client::accessToken('XXXXXXXXXXXXXXXXXX'); // Insert Asana Access Token

$task_id = 'XXXXXXXXXXXX'; // insert the task id you want to upload a file to
$message = '';
if (isset($_POST['uploadBtn']) && $_POST['uploadBtn'] == 'Upload') {
	
  if (isset($_FILES['uploadedFile']) && $_FILES['uploadedFile']['error'] === UPLOAD_ERR_OK) {
    // get details of the uploaded file
    $fileTmpPath = $_FILES['uploadedFile']['tmp_name'];
    $fileName = $_FILES['uploadedFile']['name'];
    $fileNameCmps = explode(".", $fileName);
    $fileExtension = strtolower(end($fileNameCmps));

    // check if file has one of the following extensions
    $allowedfileExtensions = array('jpg', 'gif', 'png', 'zip', 'txt', 'xls', 'doc');
	
	// get contents of the file
	$fileToUpload = file_get_contents($fileTmpPath);
 
    if (in_array($fileExtension, $allowedfileExtensions)) {
		$result = $client->attachments->createOnTask($task_id, $fileToUpload, $fileName , '');
		$message = "upload complete";
    }
    else {
		echo 'Upload failed. Allowed file types: ' . implode(', ', $allowedfileExtensions);
    }
  } else {
		echo 'There is some error in the file upload. Please check the following error.<br>';
		echo 'Error:' . $_FILES['uploadedFile']['error'];
  }
}

?>

<body>
  <form method="POST" action="upload.php" enctype="multipart/form-data">
     
	<label for="file-upload">Choose a file...</label><br><br>
	<input type="file" id="file-upload" name="uploadedFile"><br><br>
 
    <input type="submit" name="uploadBtn" value="Upload" />
  </form>
  
  <?php echo $message?>
</body>

```

---

<div class="post-metadata">

**Author:** ![Phil\_Seeman](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/phil_seeman/32/81525_2.png) [@Phil\_Seeman](https://forum.asana.com/u/Phil_Seeman)\
**Post date:** [28 July 2022 02:24 UTC](https://forum.asana.com/t/api-php-attachment/23715/9 "2022-07-28T02:24:51Z")

</div>

Thanks for posting that code, @Shane8! You can be sure it will benefit someone down the road.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/asana/user_avatar/forum.asana.com/system/32/276370_2.png) [@system](https://forum.asana.com/u/system)\
**Post date:** [30 May 2025 19:03 UTC](https://forum.asana.com/t/api-php-attachment/23715/10 "2025-05-30T19:03:46Z")

</div>

Closed on 30 May 2025 19:03 UTC


