通过 PHP 和 HTML 表单将文件上传到 ownCloud
Uploading a file to ownCloud through PHP and a HTML Form
我在尝试开发一个 web 应用程序以通过使用 PHP 的表单将文件上传到我自己的云服务器时遇到了一些麻烦,我正在使用 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);
?>
当我尝试上传文件时,我收到了这样的回复:
Sabre\DAV\Exception\NotAuthenticated No 'Authorization: Basic' header
found. Either the client didn't send one, or the server is
mis-configured
但是当我使用 owncloud 客户端时,我可以毫无问题地访问我的文件。
编辑:将名称变量 $ch 更正为 $request 并添加行:
curl_setopt($request,CURLOPT_HTTPHEADER,'Authorization: Basic');
来自@Craig post,之后我收到此错误消息:
Sabre\DAV\Exception\Conflict PUT is not allowed on non-files.
请帮我解决这个问题。问候 :D
在你的 curl 选项中包括这个:
CURLOPT_HTTPHEADER => array('Authorization: Basic');
或使用您现有的惯例:
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Authorization: Basic');
最后,为了通过 owncloud 管理我的文件,我不得不将表单指向网络服务器上的一个目录,然后使用 owncloud 插件安装外部存储源,对我来说效果很好。
使用 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);
我在尝试开发一个 web 应用程序以通过使用 PHP 的表单将文件上传到我自己的云服务器时遇到了一些麻烦,我正在使用 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);
?>
当我尝试上传文件时,我收到了这样的回复:
Sabre\DAV\Exception\NotAuthenticated No 'Authorization: Basic' header found. Either the client didn't send one, or the server is mis-configured
但是当我使用 owncloud 客户端时,我可以毫无问题地访问我的文件。
编辑:将名称变量 $ch 更正为 $request 并添加行:
curl_setopt($request,CURLOPT_HTTPHEADER,'Authorization: Basic');
来自@Craig post,之后我收到此错误消息:
Sabre\DAV\Exception\Conflict PUT is not allowed on non-files.
请帮我解决这个问题。问候 :D
在你的 curl 选项中包括这个:
CURLOPT_HTTPHEADER => array('Authorization: Basic');
或使用您现有的惯例:
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Authorization: Basic');
最后,为了通过 owncloud 管理我的文件,我不得不将表单指向网络服务器上的一个目录,然后使用 owncloud 插件安装外部存储源,对我来说效果很好。
使用 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);