對 PUT 方法的支持

PHP 對部分客戶端具備的 HTTP PUT 方法提供了支持。PUT 請求比文件上傳要簡單的多,它們一般的形式為:

PUT /path/filename.html HTTP/1.1

這通常意味著遠程客戶端會將其中的 /path/filename.html 存儲到 web 目錄樹。讓 Apache 或者 PHP 自動允許所有人覆蓋 web 目錄樹下的任何文件顯然是很不明智的。因此,要處理類似的請求,必須先告訴 web 服務(wù)器需要用特定的 PHP 腳本來處理該請求。在 Apache 下,可以用 Script 選項來設(shè)置。它可以被放置到 Apache 配置文件中幾乎所有的位置。通常我們把它放置在 <Directory> 區(qū)域或者 <VirtualHost> 區(qū)域??梢杂萌缦乱恍衼硗瓿稍撛O(shè)置:

Script PUT /put.php

這將告訴 Apache 將所有對 URI 的 PUT 請求全部發(fā)送到 put.php 腳本,這些 URI 必須和 PUT 命令中的內(nèi)容相匹配。當(dāng)然,這是建立在 PHP 支持 .php 擴展名,并且 PHP 已經(jīng)在運行的假設(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($putdata1024))
  
fwrite($fp$data);

/* Close the streams */
fclose($fp);
fclose($putdata);
?>