[使用php和curl将文件上传到户外

问题描述 投票:1回答:2

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

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

这会将文件test.doc上传到上载目录,并将其重命名为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"}}

显然,有很多可用空间。任何想法?谢谢

php curl alfresco
2个回答
1
投票

您的第一个错误是使用@方案,自PHP 5.5起不建议使用,在PHP 5.6中默认禁用,在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,带有一个lying内容类型标头,这是您不应该手动设置标头的至少两个原因之一,另一个是您可能有错字,libcurl不会(由于自动的libcurl发布单元测试)

这里没有第四个错误,但是服务器开发人员(alfresco devs?),服务器应该以HTTP 400 Bad Request响应进行响应,但是以一些虚假的out of disk space错误作为响应,您应该提交错误报告与服务器开发人员一起。

第五个错误是您的错误,您忘记了在PHP代码中使用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 );

0
投票

这不返回任何东西。我究竟做错了什么?谢谢

© www.soinside.com 2019 - 2024. All rights reserved.