MehdiB
MehdiB

Reputation: 906

upload files to alfresco using php and curl

I'm trying to upload files to alfresco using php and curl. I can upload files by running the following form command line:

curl -uadmin:admin -X POST http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-shared-/children -F [email protected] -F name=myfile.doc -F relativePath=uploads

this uploads the file test.doc to the uploads directory and renames it to myfile.doc.

Now I am trying to translate this command in php. this is what I did:

$url = 'http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-shared-/children?alf_ticket=TICKET_66....';
$fields = array(
    'filedata' => '@'.realpath('tmp_uploads/test.doc'),
    'name' => 'myfile.doc',
    'relativePath' => 'uploads'
);

$converted_fields = http_build_query($fields);

$ch = curl_init();

//set options
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-type: multipart/form-data"
));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $converted_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //needed so that the $result=curl_exec() output is the file and isn't just true/false

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

//close connection
curl_close($ch);

However, this is not working and throws the following error, which is not very descriptive.

{"error":{"errorKey":"No disk space available","statusCode":409,"briefSummary":"02280051 No disk space available","stackTrace":"For security reasons the stack trace is no longer displayed, but the property is kept for previous versions","descriptionURL":"https://api-explorer.alfresco.com"}}

Apparently there'S lots of space available. any idea? Thanks

Upvotes: 1

Views: 1115

Answers (1)

hanshenrik
hanshenrik

Reputation: 21463

your first mistake is using the @ scheme, it was discouraged as of PHP 5.5, disabled-by-default in PHP 5.6, and completely removed in PHP7. use CURLFile instead of @.

your second mistake, is using http_build_query, which will encode the data in application/x-www-form-urlencoded format, while your curl command line uploads it in multipart/form-data format

your third mistake is setting the header Content-Type: multipart/form-data manually, don't do that, curl will do it for you (and as your code is right now, it's not multipart/form-data at all, but application/x-www-form-urlencoded, with a lying content-type header, this is 1 of at least 2 reasons you should not set the header manually, the other being that you may have a typo, libcurl won't (thanks to automated libcurl release unit tests))

the fourth mistake here is not on you, but the server devs (alfresco devs?), the server should have responded with a HTTP 400 Bad Request response, but instead responded with some bogus out of disk space error, you should file a bug report with the server devs.

the fifth mistake is yours, you forgot to set the username/password with CURLOPT_USERPWD in the php code. try

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

$ch = curl_init ();
curl_setopt_array ( $ch, array (
        CURLOPT_USERPWD => 'admin:admin',
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => array (
                'filedata' => new CURLFile ( 'tmp_uploads/test.doc' ),
                'name' => 'myfile.doc',
                'relativePath' => 'uploads' 
        ),
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true 
) );

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

// close curl handle
curl_close ( $ch );

Upvotes: 1

Related Questions