對 PUT 方法的支援
PHP 對部分客戶端具備的 HTTP PUT 方法提供了支援。PUT 請求比檔案上傳要簡單的多,它們一般的形式為:
PUT /path/filename.html HTTP/1.1
這通常意味著遠端客戶端會將其中的 /path/filename.html
儲存到 web 目錄樹。讓 Apache 或者 PHP 自動允許所有人覆蓋
web 目錄樹下的任何檔案顯然是很不明智的。因此,要處理類似的請求,必須先告訴
web 伺服器需要用特定的 PHP 指令碼來處理該請求。在 Apache 下,可以用
Script 選項來設定。它可以被放置到
Apache 配置檔案中幾乎所有的位置。通常我們把它放置在
<Directory>
區域或者 <VirtualHost>
區域。可以用如下一行來完成該設定:
Script PUT /put.php
這將告訴 Apache 將所有對 URI 的 PUT 請求全部發送到 put.php 指令碼,這些 URI 必須和 PUT 命令中的內容相匹配。當然,這是建立在 PHP 支援 .php 副檔名,並且 PHP 已經在執行的假設之上。 The destination resource for all PUT requests to this script has to be the script itself, not a filename the uploaded file should have.
With PHP you would then do something like the following in your put.php. This would copy the contents of the uploaded file to the file myputfile.ext on the server. You would probably want to perform some checks and/or authenticate the user before performing this file copy.
示例 #1 儲存 HTTP PUT 檔案
<?php
/* PUT data comes in on the stdin stream */
$putdata = fopen("php://input", "r");
/* Open a file for writing */
$fp = fopen("myputfile.ext", "w");
/* Read the data 1 KB at a time
and write to the file */
while ($data = fread($putdata, 1024))
fwrite($fp, $data);
/* Close the streams */
fclose($fp);
fclose($putdata);
?>