Implementing a WebDAV filesystem with PHP and SabreDAV
Locking
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.
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.
The following listing shows how to instantiate the lock backend, then pass it to the locking plug-in.
require_once('Sabre.autoload.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(); $server->addPlugin( new Sabre_DAV_Locks_Plugin($backend) ); $server->exec();
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.




