繁体   English   中英

使用PHP在我的Owncloud服务器上上传文件

[英]Upload a file on my Owncloud server with PHP

最近我创建了自己的云服务器,我需要能够从php表单上传文件,将文件从我的电脑传输到我自己的云服务器。 所以我尝试使用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_INFILECURLOPT_INFILESIZE而不是CURLOPT_POSTFIELDS

谢谢你的帮助。 //

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM