Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Post Multiple Files as Attachments Using Confluence REST API with PHP curl

Coline Macorol April 2, 2020

Hello All,

I posted a similar question as to how to post a single file using the REST API with php curl. I was able to get that to work correctly with this code:

$ch = curl_init();
$postFile = __DIR__ . '\afile.txt';
$cFile = curl_file_create($postFile);

$postData = array('file' => $cFile);
$headers = array();
$headers[] = "X-Atlassian-Token: no-check";

curl_setopt($ch, CURLOPT_URL, "http://localhost:8090/rest/api/content/31391749/child/attachment");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 1);
curl_setopt($ch, CURLOPT_USERPWD, "username" . ":" . "password");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

curl_exec($ch);
curl_close($ch);

 

Now I'm trying to post multiple files at once, and my attempts have been unsuccessful. This is what I have so far:

$ch = curl_init();

$postFiles = array(__DIR__ . '\a.txt', __DIR__ . '\b.txt',);
$postData = array();

foreach($postFiles as $index => $postFile) {
$cFile = curl_file_create($postFile);
$postData[$index] = $cFile;
}

$postData = array('file' => $cFile);
$headers = array();
$headers[] = "X-Atlassian-Token: no-check";

curl_setopt($ch, CURLOPT_URL, "http://localhost:8090/rest/api/content/31391749/child/attachment");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 1);
curl_setopt($ch, CURLOPT_USERPWD, "username" . ":" . "password");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

curl_exec($ch);
curl_close($ch);

With this code, it only posts the b.txt file.

 

I've been able to do it with cURL, but I need to do it with php curl. Anyone with any ideas or suggestions on how to amend the code above to enable the posting of multiple files? Thanks in advance!

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events