AnsweredAssumed Answered

Upload a file in Alfresco Repository using php and curl

Question asked by giancarlo on Dec 17, 2015
Latest reply on Mar 28, 2018 by adrie7tp1
I found in Alfresco web script lists

File upload
POST /alfresco/s/api/upload

and I'm tring to upload a file from local apache web server to remote alfresco 5.0.d repository.
I use a very simple PHP script:

$server_port = "8080"; // Alfresco port
$urlws = $server_ip ."/". $service_url ."api/upload?alf_ticket=". $ticket; // URL to Alfresco upload web script

$file = 'ico_html.jpg'; // File to upload

$postvars = array(
'filename' => $file,
'filedata' => '@'. realpath($file),
// destination =  node reference of the Alfresco folder I'd like to use. I used Alfresco Node brower to find it.
'destination' => 'workspace://SpacesStore/1ec885f5-bc0d-450a-9d65-a70a888e345d',
'uploaddirectory' =>'/',
'description' => 'File example',
'contenttype' => 'cm:content',
'overwrite' => 'true',

$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);   
curl_setopt($ch, CURLOPT_PORT, $server_port);
curl_setopt($ch, CURLOPT_URL, $urlws);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
$connection = curl_exec($ch);

Conection is ok but alfresco response is:

    "code" : 400,
    "name" : "Bad Request",
    "description" : "Request sent by the client was syntactically incorrect."

I tried in a different way adding json instead of $postvars array:

$json = json_encode($postvars);

but with or without command

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));

the result is the same:

"code" : 500,
"name" : "Internal Error",
"description" : "An error inside the HTTP server which prevented it from fulfilling the request."
"message" : "11170048 Unexpected error occurred during upload of new content.",

Have somebody some working solution?
Thanks al lot