我正在尝试实现一个基于 PHP 的 WebDAV 服务器,以便能够在 OS Windows(更准确地说是 Windows 7)下使用它。
一切正常,除了 PUT 方法。当我尝试将文件从文件系统上传到 WebDAV 服务器时,请求内容长度为空,这意味着我无法读取要放入服务器的文件。
这是我使用 Windows 拖动文件(简单的 .txt 文件)时收到的内容:
[HTTP_USER_AGENT] => Microsoft-WebDAV-MiniRedir/6.1.7601
[HTTP_TRANSLATE] => f
[CONTENT_LENGTH] => 0
[HTTP_VIA] => 1.1 squid.Sole:3128 (squid/2.7.STABLE9), 1.0 squidcache.Proxy6.CCSP:3129 (squid/2.6.STABLE24)
[PATH] => /bin:/usr/bin:/sbin:/usr/sbin
[SERVER_SOFTWARE] => Apache
[SERVER_PORT] => 80
[GATEWAY_INTERFACE] => CGI/1.1
[SERVER_PROTOCOL] => HTTP/1.0
[REQUEST_METHOD] => PUT
使用专用的 webdav 客户端上传相同的文件,我得到一个有效的响应并且文件正在上传。这是我使用 CyberDuck 客户端得到的结果:
[HTTP_EXPECT] => 100-continue
[CONTENT_LENGTH] => 263
[CONTENT_TYPE] => text/plain
[HTTP_CONNECTION] => Keep-Alive
[HTTP_USER_AGENT] => Cyberduck/4.2.1 (9350) (Windows 7/6.1) (x86)
[HTTP_ACCEPT_ENCODING] => gzip,deflate
[PATH] => /bin:/usr/bin:/sbin:/usr/sbin
[SERVER_SOFTWARE] => Apache
[SERVER_PORT] => 80
[GATEWAY_INTERFACE] => CGI/1.1
[SERVER_PROTOCOL] => HTTP/1.1
[REQUEST_METHOD] => PUT
CONTENT-LENGTH 字段已更改,并且文件在服务器上在线,就像我应该的那样。我将在这里发布我的代码片段:
$options = Array();
$options["path"] = $this->path;
$options["content_length"] = $_SERVER["CONTENT_LENGTH"];
$options["stream"] = fopen('php://input', 'r');
$stat = $this->PUT($options); // In the PUT method I fopen the destination file in "w"
if ($stat === false) {
$stat = "403 Forbidden";
} else if (is_resource($stat) && get_resource_type($stat) == "stream") {
$stream = $stat;
$stat = $options["new"] ? "201 Created" : "204 No Content";
if (!empty($options["ranges"])) {
if (0 == fseek($stream, $range[0]["start"], SEEK_SET)) {
$length = $range[0]["end"]-$range[0]["start"]+1;
if (!fwrite($stream, fread($options["stream"], $length))) {
$stat = "403 Forbidden";
}
} else {
$stat = "403 Forbidden";
}
}else {
while (!feof($options["stream"])) {
if (false === fwrite($stream, fread($options["stream"], 4096))) { //The fread reads nothing from the stream...
$stat = "403 Forbidden";
break;
}
}
}
fclose($stream);
}
我能做些什么来解决这个问题?有什么东西可以在 Windows 中设置以使其工作还是只是我的代码缺少一些东西?