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.
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:
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.
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.
require_once('Sabre.autoload.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.
// ... $server = new Sabre_DAV_Server($tree); $server->exec();
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.