Jira attach file to issue with PHP and CURL

Justin Lyson June 8, 2017

I have found several examples on how to upload an attachment to an issue in jira, however I have not been able to make any of them work. I posted this question on the Jira Community Help Forums but it has been over a week with 0 replies so hoping the community here can help.

Here is my current attempt:

 $Jirausername = 'myUsername';    $Jirapassword = 'myPassword';    $ch=curl_init();$headers = array(
    'X-Atlassian-Token: nocheck',
    'Content-Type: multipart/form-data'
);$data = array('file' => "testing.txt");
curl_setopt_array(    $ch,    array(        CURLOPT_URL=>'https://myCompany.net/rest/api/latest/issue/TAG-78/attachments',        CURLOPT_POST=>true,        CURLOPT_VERBOSE=>1,        CURLOPT_POSTFIELDS=>$data,        CURLOPT_SSL_VERIFYHOST=> 0,        CURLOPT_SSL_VERIFYPEER=> 0,        CURLOPT_RETURNTRANSFER=>true,        CURLOPT_HEADER=>false,        CURLOPT_HTTPHEADER=> $headers,        CURLOPT_USERPWD=>"$Jirausername:$Jirapassword"
    )
);$result=curl_exec($ch);$ch_error = curl_error($ch);
if ($ch_error) {    echo "cURL Error: $ch_error";
} else {    var_dump($result);
}curl_close($ch);

testing.txt is in the same directory as this file. I have curl installed on the webserver where this is hosted, can create issues in jira fine, just cant seem to upload attahcments...

When I run this page it displays:

string(0) "" 

No attachment is uploaded as well needless to say. Any ideas what I am doing wrong?

EDIT: here are some things I have tried:

  1. trying both nocheck and no-check
  2. trying both @testing.txt and testing.txt
  3. removing 'Content-Type: multipart/form-data'
  4. Full paths as such: $data = array('file'=>"@C:\xampp\htdocs\Website\testing.txt ,'filename'=>'testing.txt');
  5. Tried like this too because of a known curl error: $data = array('file'=>"@C:\xampp\htdocs\Website\testing.txt" ';filename=testing.txt');

And every combination of those above. No matter what I try it does not work though. Have also ensured I am an admin level user in Jira. I feel like my code should work... but clearly not.

2 answers

0 votes
irshad mk July 5, 2022

i have same issue, return string " ",   how to solve this? you can help me?

0 votes
RianS
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 3, 2017

Looks like you haven't loaded the file's data into the postbody to send with the web request. Here's a link to a PHP file for a REST request using curl that handles a file attachment. Look at the buildpostbody function.

Part of the attachment process is loading the file content and adding it to the postbody as a string. It also provides the correct Content identifier as well based on the file contents. Just providing the filename to the curl function isn't sufficient.

Suggest an answer

Log in or Sign up to answer