PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » HTTP » HTTP_WebDAV_Client » Bug #996

WebDAV file is not truncated when fopen()'ing with a mode of "w".

Details

Submitted2004-03-11 11:57 UTC
Froms dot binge at codefusion dot co dot za
Assignedhholzgra
StatusClosed
PackageHTTP_WebDAV_Client
PHP Version4.3.4
OSAny
Roadmaps(Not assigned)

Comments

[2004-03-11 11:57 UTC] s dot binge at codefusion dot co dot za

Description:
------------
When opening a file via WebDAV, and specifying a mode containing "w" (i.e. to truncate the file to zero length and open for writing), the file is opened normally but NOT truncated, meaning all the old data still exists.

Reproduce code:
---------------
// Asuming file.dat has contents "Hello World"

$fh = fopen("webdav://my.webdav.server/file.dat", 'w');
fwrite($fh, "BLAH");
fclose($fh);

Expected result:
----------------
The WebDAV file "webdav://my.webdav.server/file.dat" should contain the contents "BLAH".

Actual result:
--------------
The WebDAV file "webdav://my.webdav.server/file.dat" instead contains the contents "BLAHo World", which is not what is expected.

[2004-03-11 12:01 UTC] s dot binge at codefusion dot co dot za

A possible solution is to send an empty PUT request on an open command, when a mode containing 'w' is specified. This has the effect of truncating the file to zero length.

--- Stream.php.old 2003-12-20 03:00:15.000000000 +0200
+++ Stream.php 2004-03-05 14:24:25.000000000 +0200
@@ -176,6 +176,28 @@

if (strpos($mode, "a") !== false) {
$this->eof = true;
+ } else if (strpos($mode, "w") !== false) {
+ // Truncate the file (i.e. PUT a zero-length file)
+ $req = &new HTTP_Request($this->url);
+ $req->setMethod(HTTP_REQUEST_METHOD_PUT);
+ if (is_string($this->user)) {
+ $req->setBasicAuth($this->user, @$this->pass);
+ }
+ if ($this->locktoken) {
+ $req->addHeader("If", "(<{$this->locktoken}>)");
+ }
+
+ $req->sendRequest();
+
+ switch ($req->getResponseCode()) {
+ case 200:
+ case 201:
+ case 204:
+ return true;
+
+ default:
+ return false;
+ }
}

// we are done :)