【问题标题】:Upload a file on my Owncloud server with PHP使用 PHP 在我的 Owncloud 服务器上上传文件
【发布时间】:2023-03-05 04:57:01
【问题描述】:

最近我创建了自己的云服务器,我需要能够从 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);
?>

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,奇怪......

【问题讨论】:

    标签: php curl upload server owncloud


    【解决方案1】:

    我也遇到了上传到 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

    感谢您的帮助。 //

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-05
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      相关资源
      最近更新 更多