phpcurlowncloud

Uploading a file to ownCloud through PHP and a HTML Form


I'm having some troubles trying to develop a webapp for uploading files to my owncloud server trough a form using PHP, I'm using curl to PUT a request through the webDav, so here's the code:

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

?>

When I try to upload the file I get this response:

Sabre\DAV\Exception\NotAuthenticated No 'Authorization: Basic' header found. Either the client didn't send one, or the server is mis-configured

But when I use the owncloud Client I can access to my files without problems.

EDIT: Corrected the name variable $ch to $request and added the line :

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

from @Craig post, after that I got this error message:

Sabre\DAV\Exception\Conflict PUT is not allowed on non-files.

Please help me to solve this. Regards :D


Solution

  • Finally to manage my files through owncloud I had to point the form to a directorory on the webserver and then used the owncloud plugin to mount external storage sources and works pretty fine to me.

    External Storage Configuration