通过PHP和HTML表单将文件上传到ownCloud

时间:2016-09-28 19:56:19

标签: php curl owncloud

我在尝试使用PHP开发一个用于通过表单上传文件到我自己的云服务器的webapp时遇到了一些麻烦,我正在使用curl通过webDav发送请求,所以这里是代码:

的index.php

<html>
        <head>
            <title>Theform is here</title>
        </head>
        <body>
            <div align="center">
                <h1> File uploads with OwnCloud API</h1>
                <form method="post" action="uploader.php" name="fileuploader">
                    <label>Select a File to upload</label><br>
                    <input type="file" name="file"></input><br>
                    <input type="submit" value="upload file"></input>
                </form>
                <?php
                ?>
            </div>
        </body>
    </html>

uploader.php

<?php
    $request = curl_init('http://mydomain.cl/owncloud/remote.php/webdav/Dev/');

    curl_setopt($request,CURLOPT_POST,true);
    curl_setopt($request,CURLOPT_HTTPHEADER,'Authorization: Basic');
    curl_setopt($request, CURLOPT_USERPWD, "user:password");
    curl_setopt($request, CURLOPT_PUT, 1);
    curl_setopt(
        $request,
        CURLOPT_INFILE,
        array(
            'thefile'=>
                        '@'            .$_FILES['file']['tmp_name']
                        . ';filename=' .$_FILES['file']['daName']
                        . ';type='     .$_FILES['file']['type']

        ));
    curl_setopt($request, CURLOPT_INFILE, $_FILES['file']);
    curl_setopt($request, CURLOPT_INFILESIZE, filesize($_FILES['file']));


    // output the response
    curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($request, CURLOPT_BINARYTRANSFER, TRUE); 
    echo curl_exec($request);

    // close the session
    curl_close($request);

?>

当我尝试上传文件时,我得到了这个回复:

  

Saber \ DAV \ Exception \ NotAuthenticated否'授权:基本'标题   找到。客户端没有发送一个,或者服务器是   误配置的

但是当我使用owncloud客户端时,我可以毫无问题地访问我的文件。

编辑:将名称变量$ ch更正为$ request并添加以下行:

  

curl_setopt($ request,CURLOPT_HTTPHEADER,'授权:基本');

来自@Craig帖子,之后我收到了这条错误消息:

  

Saber \ DAV \ Exception \ Conflict PUT不允许在非文件上使用。

请帮我解决这个问题。问候:D

3 个答案:

答案 0 :(得分:0)

将此包含在您的卷曲选项中:

CURLOPT_HTTPHEADER => array('Authorization: Basic');

或使用您现有的约定:

curl_setopt($ch, CURLOPT_HTTPHEADER, 'Authorization: Basic');

答案 1 :(得分:0)

最后要通过owncloud管理我的文件,我不得不将表单指向网络服务器上的一个导演,然后使用owncloud插件来安装外部存储源,对我来说工作得非常好。

External Storage Configuration

答案 2 :(得分:0)

使用html表单和php将文件上传到owncloud

<html>
        <head>
            <title>Theform is here</title>
        </head>
        <body>
            <div align="center">
                <h1> File uploads with OwnCloud API</h1>
                <form method="post" action="" name="fileuploader" enctype="multipart/form-data">
                    <label>Select a File to upload</label><br>
                    <input type="file" name="file"></input><br>
                    <input type="submit" name="submit" value="upload file"></input>
                </form>
                <?php
                ?>
            </div>
        </body>
    </html>


$file_path_str = $_FILES['file']['tmp_name'];
$filename=$_FILES['file']['name'];
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://server/owncloud/remote.php/webdav/' . basename($filename));
curl_setopt($ch, CURLOPT_FAILONERROR, true); 
curl_setopt($ch, CURLOPT_USERPWD, "user:password");
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);
if (curl_errno($ch)) {
    $error_msg = curl_error($ch);
    print_r($error_msg);exit;
}
fclose($fh_res);