Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...

When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Implementing a WebDAV filesystem with PHP and SabreDAV

Bootstrapping the WebDAV Server

Getting started with SabreDAV is extremely simple. Since we'll be storing our files directly onto the server filesystem we can use the built-in local filesystem handler.

Note: Later in this article we'll look briefly at creating virtual filesystems, such as if you wanted to store your files in a database.

In this section we'll be gradually building up the server.php mentioned earlier in this article. The full location of this file is as follows:

  • /var/www/dav/htdocs/server.php

The SabreDAV Server Class

The main class that controls the WebDAV server is the Sabre_DAV_Server. The server is created by instantiating this class, then running the exec() method on it.

When instantiating Sabre_DAV_Server you must pass a filesystem tree to it. This is an object that extends the abstract Sabre_DAV_Tree class. We'll use the Sabre_DAV_ObjectTree class for this purpose.

Sabre_DAV_ObjectTree accepts as its only argument an instance of Sabre_DAV_FS_Directory, used to specify the directory on the filesystem.

The following listing shows how to create our filesystem tree. If you're storing your files in a different directory, update the $path variable accordingly.

Listing 3 Creating the SabreDAV object tree (listing-3.php)
    $path = '/var/www/dav/files';
    $tree = new Sabre_DAV_ObjectTree(
        new Sabre_DAV_FS_Directory($path)
    // ...

Next we can instantiate Sabre_DAV_Server and pass the $tree variable to it. Once we've done that we simply call exec(). This will handle any client requests to the WebDAV server.

Listing 4 Handling the WebDAV request (listing-4.php)
    // ...
    $server = new Sabre_DAV_Server($tree);

You now have a working WebDAV server! However, our job is not yet complete - you will be able to read files, but you may not be able to write any file (depending on your platform).

In the next section I'll show you how to map this newly-created drive to your computer, and then I'll show you some plug-ins. You will need to use plug-ins to make your server useful.

In This Article