使用PHP上传我的Owncloud服务器上的文件 [英] Upload a file on my Owncloud server with PHP

查看:1870
本文介绍了使用PHP上传我的Owncloud服务器上的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我创建了我自己的云服务器,我需要能够从php表单上传文件,将文件从我的电脑传输到我自己的云服务器。所以我试着使用Curl,像这样:

Recently I created my owncloud server and I need to be able to upload a file from a php form which transfer an file from my pc to my owncloud server. So I tried to use Curl, like this :

<?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);
?>

我的灵感来自此帖子和此命令:

I have been inspire by this post and this command :

curl -X PUT "http://server.com/owncloud/remote.php/webdav/file.zip" --data-binary @"/Users/Root/Downloads/file.zip"

命令行,他工作,但不是我的PHP。我成功地上传文件,但文件已损坏,我不知道为什么:/。也许我错过MIME类型?是否足以获取损坏的文件?

The command line, he's working but not my php. I succeed to upload the file but the file is corrupted and I don't know why :/. Maybe I miss the MIME type ? Is it enough to get a corrupted file ?

你看到我错了吗?
最好的问候,Zed13

Do you see where I am wrong ? Best regards, Zed13

编辑:当我制作一个文件的上传文件,它是类型数据,不是png,奇怪...

Edit : When I make an file of my uploaded file, it's of type data and not png, strange...

推荐答案

我也遇到了一个上传到owncloud的问题。有相同的症状,命令行curl工程,但不是PHP curl调用。

I was having an issue with an upload to owncloud as well. Had the same symptoms, the command line curl works, but not the PHP curl call.

感谢你的帖子我能够得到它的工作。这是对我有用的。

Thanks to your post I was able to get it working. Here is what works for me

        // 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

谢谢你的帮助。
//

Thanks for your help. //

这篇关于使用PHP上传我的Owncloud服务器上的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆