API php attachment

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!

Hey @Artem,

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.

Hi Jeff,

here it is:

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`)

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!

2 Likes

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

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!

1 Like

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

Thanks @Artem - 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.

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

<?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>
1 Like

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

1 Like