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


In the WebDAV server we have created thus far, there is no file-locking solution. File locking is used by the WebDAV client (i.e. Windows or OS X) to ensure no other clients will modify or delete the file while it is trying to write to it.

If you are using Mac OS X, you cannot even write to your WebDAV mount unless it uses file locking (it will appear as read-only when you browse the files).

To get around this, we use the file locking plug-in that comes with SabreDAV. This plug-in is called Sabre_DAV_Locks_Plugin.

In order to instantiate it you must pass it a backend object. The backend object dictates how the lock is stored. SabreDAV comes with a backend to write lock data to the filesystem, as well as one to write lock data to the database.

Note: The database lock backend only supports PDO. If you want to store lock data in a database but you don't use PDO, you can create your own by extending the Sabre_DAV_Locks_Backend_Abstract class.

We'll use the file system lock backend. You can either pass to it the location of a temporary (writable) directory to write lock data to, or you can omit this argument and let SabreDAV deal with it.

Note: Just like the temporary file filter, you will need to ensure this directory is cleaned up after a period of time.

The following listing shows how to instantiate the lock backend, then pass it to the locking plug-in.

Listing 7 Adding file locking using the provided plug-in (listing-7.php)
    $path = '/var/www/dav/files';
    $tree = new Sabre_DAV_ObjectTree(
        new Sabre_DAV_FS_Directory($path)
    $server = new Sabre_DAV_Server($tree);
    $backend = new Sabre_DAV_Locks_Backend_FS();
        new Sabre_DAV_Locks_Plugin($backend)

Now your WebDAV server has locking support. This means you can use it as a drive on your computer! To try this out, copy a file from your computer to the WebDAV drive. You can then try renaming, moving or deleting the file.

The only problem now is that it is wide open for anybody to use. Next we'll take a look at how you can add authentication to your web drive.

In This Article