PHP 對(duì)部分客戶(hù)端具備的 HTTP PUT 方法提供了支持。PUT 請(qǐng)求比文件上傳要簡(jiǎn)單的多,它們一般的形式為:
PUT /path/filename.html HTTP/1.1
這通常意味著遠(yuǎn)程客戶(hù)端會(huì)將其中的 /path/filename.html
存儲(chǔ)到 web 目錄樹(shù)。讓 Apache 或者 PHP 自動(dòng)允許所有人覆蓋
web 目錄樹(shù)下的任何文件顯然是很不明智的。因此,要處理類(lèi)似的請(qǐng)求,必須先告訴
web 服務(wù)器需要用特定的 PHP 腳本來(lái)處理該請(qǐng)求。在 Apache 下,可以用
Script 選項(xiàng)來(lái)設(shè)置。它可以被放置到
Apache 配置文件中幾乎所有的位置。通常我們把它放置在
<Directory>
區(qū)域或者 <VirtualHost>
區(qū)域??梢杂萌缦乱恍衼?lái)完成該設(shè)置:
Script PUT /put.php
這將告訴 Apache 將所有對(duì) URI 的 PUT 請(qǐng)求全部發(fā)送到 put.php 腳本,這些 URI 必須和 PUT 命令中的內(nèi)容相匹配。當(dāng)然,這是建立在 PHP 支持 .php 擴(kuò)展名,并且 PHP 已經(jīng)在運(yùn)行的假設(shè)之上。 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);
?>