最近我创建了我的 owncloud 服务器,我需要能够从 php 表单上传文件,该表单将文件从我的电脑传输到我的 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);
?>
this post 启发了我这个命令:
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
感谢您的帮助。 //
关于php - 使用 PHP 在我的 Owncloud 服务器上上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30319911/