PhpRiot
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

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.

Note: We're only using non-SSL in this article to keep set up somewhat straightforward. If you have a secure server available, you should use it if you can.

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 site
  • include - This is our PHP include directory, where we will download SabreDAV to
  • files - 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.

Listing 1 Enabling mod_rewrite so a single script handles all requests (listing-1.txt)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/*(.*)$ /server.php/$1

Now we can create the virtual host. The following listing shows how the virtual host may look on a typical Apache setup.

Listing 2 Apache virtual host for the WebDAV server (listing-2.txt)
<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.

In This Article