![](/img/trans.png)
[英]Uploading a file from a local .net application through the HTML Form file upload feature (php server side)
[英]Uploading a file to ownCloud through PHP and a HTML Form
我在嘗試開發一個 web 應用程序以使用 PHP 通過表單將文件上傳到我自己的雲服務器時遇到了一些麻煩,我使用 curl 通過 webDav 發送請求,所以這里是代碼:
索引.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>
上傳者.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 更正為 $request 並添加以下行:
curl_setopt($request,CURLOPT_HTTPHEADER,'授權:基本');
來自@Craig 的帖子,之后我收到了這個錯誤信息:
Sabre\\DAV\\Exception\\Conflict PUT 不允許用於非文件。
請幫我解決這個問題。 問候:D
在您的curl選項中包括以下內容:
CURLOPT_HTTPHEADER => array('Authorization: Basic');
或使用您現有的約定:
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Authorization: Basic');
最終,要通過owncloud管理我的文件,我不得不將表單指向Web服務器上的目錄,然后使用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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.