首页 文章

使用 php 和 curl 将文件上传到 alfresco

提问于
浏览
1

我正在尝试使用 php 和 curl 将文件上传到 alfresco。我可以通过运行以下表单命令行来上传文件:

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

这会将文件 test.doc 上传到 uploads 目录并将其重命名为 myfile.doc。

现在我试图在 php 中翻译这个命令。这就是我做的:

$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);

但是,这不起作用并抛出以下错误,这不是非常具有描述性。

{"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"}}

显然有很多空间可供选择。任何的想法?谢谢

1 回答

  • 1

    你的第一个错误是使用@方案,因为 PHP 5.5,PHP 5.6 中的 disabled-by-default,并且在 PHP7 中完全删除了它。使用CURLFile而不是@。

    你的第二个错误是使用 http_build_query,它将以application/x-www-form-urlencoded格式编码数据,而你的 curl 命令行以multipart/form-data格式上传它

    你的第三个错误就是手动设置标题Content-Type: multipart/form-data,不要这样做,curl 会为你做(并且你的代码现在正好,它根本不是multipart/form-data,而是application/x-www-form-urlencoded,****** content-type 标题,这是你不应该手动设置标题的至少 2 个原因之一,另一个是你可能有拼写错误,libcurl 不会(感谢自动 libcurl 发布单元 tests))

    这里的第四个错误不在你身上,但是服务器 devs(alfresco devs?),服务器应该用HTTP 400 Bad Request响应响应,但是反应有一些假的out of disk space错误,你应该向服务器 devs 提交 bug 报告。

    第五个错误是你的,你忘了在 php 代码中设置 username/password 和 CURLOPT_USERPWD。尝试

    $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 );
    

相关问题