cancel
Showing results for 
Search instead for 
Did you mean: 

Error uploading file with JSON request

ni0ni0s
Champ in-the-making
Champ in-the-making

i have this code but i can't upload file into enywhere.
can you give an example of jsonData array syntax?
i have the default installation of alfresco and i have achieved to create direcory.
i cant find the way to upload file.
thanks

<?php

//API Url
$url = '192.168.1.1:8080/alfresco/s/api/upload?alf_ticket=TICKET_e0113b837d77e99b3e0a33da110e12be52bded4e';

//Initiate cURL.
$ch = curl_init($url);

//The JSON data.
$jsonData = array(
"filedata"=> "test.txt",
"siteid"=> "siteName",
"containerid"=> "documentLibrary",
"description"=> "A shiny new node"
);

//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);

//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

//Execute the request
$result = curl_exec($ch);

?>

3 REPLIES 3

angelborroy
Community Manager Community Manager
Community Manager

This is a working sample using the new REST API:

https://stackoverflow.com/questions/43072054/upload-files-to-alfresco-using-php-and-curl

Hyland Developer Evangelist

ni0ni0s
Champ in-the-making
Champ in-the-making

thanks for your answer

but which is the relativePath?

 'relativePath' => 'uploads' 

ni0ni0s
Champ in-the-making
Champ in-the-making

have you done this?? is this a working example for sure?

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


$url = 'http://192.168.1.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-shared-/children?al...';

$ch = curl_init();

$charray = array(
CURLOPT_USERPWD => 'admin:admin',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'filedata' => new CURLFile('test.txt'),
'name' => 'test.txt',
'relativePath' => 'Sites/site1/documentLibrary'
),
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true
);

curl_setopt_array($ch,$charray);

// execute post
$result = curl_exec($ch);

// close curl handle
curl_close($ch);

?>

the above returns nothing.