Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP在我自己的云服务器上上传文件_Php_Curl_Upload_Server_Owncloud - Fatal编程技术网

使用PHP在我自己的云服务器上上传文件

使用PHP在我自己的云服务器上上传文件,php,curl,upload,server,owncloud,Php,Curl,Upload,Server,Owncloud,最近我创建了我自己的云服务器,我需要能够从php表单上传一个文件,该表单将文件从我的pc传输到我自己的云服务器。所以我试着用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"); //

最近我创建了我自己的云服务器,我需要能够从php表单上传一个文件,该表单将文件从我的pc传输到我自己的云服务器。所以我试着用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);
?>
命令行,他正在工作,但不是我的php。我成功上传文件,但文件已损坏,我不知道原因:/。也许我错过了哑剧类型?获取损坏的文件是否足够

你知道我错在哪里吗? 致以最良好的问候,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\u PUT
    而不是
    CURLOPT\u CUSTOMREQUEST
  • CURLOPT_infle
    CURLOPT_inflesize
    而不是
    CURLOPT_POSTFIELDS
谢谢你的帮助。 //

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