Reputation: 134
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
Upvotes: 0
Views: 2495
Reputation: 524
To upload file to owncloud with html form and php
<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);
Upvotes: 0
Reputation: 134
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
Upvotes: 0
Reputation: 444
Include this in your curl options:
CURLOPT_HTTPHEADER => array('Authorization: Basic');
or to use your existing convention:
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Authorization: Basic');
Upvotes: 0