Implementing a WebDAV filesystem with PHP and SabreDAV
Creating a New Virtual Host
Let's now set up a virtual host for our WebDAV server. This article assumes you are using Apache HTTP server on a Unix-based platform. If you are not then you may need to make some adjustments. We'll be setting up a non-SSL server.
As discussed in the previous section, we must set up our WebDAV server on the root directory of a web host. There are also some other restrictions when it comes to Microsoft Windows:
- The server must use standard port numbers (80 for non-SSL, 443 for SSL)
- Basic HTTP authentication cannot be used for non-SSL sites
We'll discuss authentication later in this article. For now, let's set up a new virtual host. We're going set up our host with three primary directories:
htdocs- This is the root directory of our web siteinclude- This is our PHP include directory, where we will download SabreDAV tofiles- This is where files managed via WebDAV will be stored.files/dav- To be compatible with Windows we'll only store files within this sub-directory.
For the purposes of this article we'll assume all of these directories reside within the /var/www/dav directory. The directories in full are as follows:
/var/www/dav/htdocs/var/www/dav/include/var/www/dav/files/var/www/dav/files/dav
Since our site must be at the root of the web host, but also handle requests from the dav directory, we're going to use Apache's mod_rewrite to redirect all requests to a single script. We'll create this script later - for now all your need to know is that it's called server.php and it resides in /var/www/dav/htdocs
The following listing shows how to enable mod_rewrite and direct all requests through the server.php file. If the requested file exists then it is served normally, but if it doesn't then server.php handles the request instead.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/*(.*)$ /server.php/$1Now we can create the virtual host. The following listing shows how the virtual host may look on a typical Apache setup.
<VirtualHost *:80>
ServerName dav.example.com
DocumentRoot /var/www/dav/htdocs
php_value include_path /var/www/dav/include
<Directory /var/www/dav/htdocs>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/*(.*)$ /server.php/$1
</Directory>
</VirtualHost>Once you have set this host up and restarted your server you can start implementing your WebDAV server.
File Permissions
Earlier in this section we created a directory called /var/www/dav/files. This is the location on the WebDAV server where files managed via WebDAV are stored.
That is, when a WebDAV user copies a file to their mounted drive, it will be stored in this directory.
Therefore, this directory must be writable by your web server.




