It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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
Community showcase
Posted in Confluence

Lessons and Learnings: Six Months of Working Remote [Discussion]

Hey there, folks! For most of us, the past six months- yes, you read that right- have been a journey. More people than ever before have pivoted to working remotely, and navigating being on-scre...

2,117 views 4 5
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you