最近,我创建了我的owncloud服务器,我需要能够从php表单上传文件,该文件将文件从我的PC传输到我的owncloud服务器。因此,我尝试使用Curl,如下所示:
<?php
$url = "5.25.9.14/remote.php/webdav/plus.png";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // -X PUT
curl_setopt($ch, CURLOPT_USERPWD, "root:root"); // --user
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); // --data-binary
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'img/plus.png' => '@'.realpath('img/plus.png')
)
);
$output = curl_exec($ch);
curl_close($ch);
?>
这个帖子和这个命令给我启发:
curl -X PUT "http://server.com/owncloud/remote.php/webdav/file.zip" --data-binary @"/Users/Root/Downloads/file.zip"
命令行,他正在工作,但不是我的PHP。我成功上传了文件,但是文件已损坏,我也不知道为什么:/。也许我错过了MIME类型?是否足以获取损坏的文件?
看到我错了吗?最好的问候,Zed13
编辑:当我使我的上传文件的文件,它的类型数据,而不是png,奇怪的是...
我在上传到owncloud时也遇到了问题。具有相同的症状,命令行curl可以工作,但PHP curl调用不行。
多亏了您的帖子,我才能够正常工作。这对我有用
// upload backup
$file_path_str = '/tmp/' . date('Ymd') . '.tar.gz';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://server/remote.php/webdav/backups/' . basename($file_path_str));
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt($ch, CURLOPT_PUT, 1);
$fh_res = fopen($file_path_str, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fh_res);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path_str));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); // --data-binary
$curl_response_res = curl_exec ($ch);
fclose($fh_res);
不同之处在于:
CURLOPT_PUT
代替 CURLOPT_CUSTOMREQUEST
CURLOPT_INFILE
而CURLOPT_INFILESIZE
不是CURLOPT_POSTFIELDS
谢谢你的帮助。//
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句