我遇到了一些麻煩,試圖開發一個webapp上傳文件到我的雲服務器槽通過使用PHP的窗體,我使用curl通過webDav PUT請求,所以這裏的代碼:通過PHP和HTML表單將文件上傳到ownCloud
的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否'授權:基本'標題 找到。無論是客戶端沒有發送一個,或服務器 錯誤配置
但是,當我使用owncloud客戶端,我可以訪問我的文件沒有問題。
編輯:更正名稱變量$ CH到$請求和添加的行:
curl_setopt($請求,CURLOPT_HTTPHEADER, '授權:基本');
從@Craig後,在那之後我得到這個錯誤信息:
軍刀\ DAV \異常\ PUT衝突不允許對非文件。
請幫我解決這個問題。問候:d
編輯:瀏覽的文檔上我發現了一個CURL方法張貼文件,但似乎是命令行[OwnCloud文檔](https://doc.owncloud.org/server/9.1/user_manual/files/ access_webdav.html #access-files-using-curl),所以如果有人知道如何解析ir或在php上使用它將對我的目標有很大的幫助。 – NMenam