[英]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:我在尝试开发一个 web 应用程序以使用 PHP 通过表单将文件上传到我自己的云服务器时遇到了一些麻烦,我使用 curl 通过 webDav 发送请求,所以这里是代码:
Index.php索引.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
<?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.
Sabre\\DAV\\Exception\\NotAuthenticated 未找到“授权:基本”标头。 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.但是当我使用 owncloud 客户端时,我可以毫无问题地访问我的文件。
EDIT: Corrected the name variable $ch to $request and added the line :编辑:将名称变量 $ch 更正为 $request 并添加以下行:
curl_setopt($request,CURLOPT_HTTPHEADER,'Authorization: Basic');
curl_setopt($request,CURLOPT_HTTPHEADER,'授权:基本');
from @Craig post, after that I got this error message:来自@Craig 的帖子,之后我收到了这个错误信息:
Sabre\\DAV\\Exception\\Conflict PUT is not allowed on non-files.
Sabre\\DAV\\Exception\\Conflict PUT 不允许用于非文件。
Please help me to solve this.请帮我解决这个问题。 Regards :D
问候:D
Include this in your curl options: 在您的curl选项中包括以下内容:
CURLOPT_HTTPHEADER => array('Authorization: Basic');
or to use your existing convention: 或使用您现有的约定:
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Authorization: Basic');
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. 最终,要通过owncloud管理我的文件,我不得不将表单指向Web服务器上的目录,然后使用owncloud插件挂载外部存储源,并且对我来说还算不错。
To upload file to owncloud with html form and php使用 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.