Checkout SVN with PHP

by Vincenzo Gambino

Subversion (SVN) is a version control system that allows multiple developers to work on code simultaneously without coming into conflict (further information on SVN can be found on Wikipedia). We find ourselves working on ever more complex sites and SVN has become a very useful tool that, if required, allows all staff to work on a project at any one time. We use a windows SVN client called Tortoise SVN so that we can work remotely on code and then commit to a central SVN repository.

When it comes to updating a site by checking out the code from a repository we usually run commands via an SSH shell connection, however we have found recently that when we are working with external companies (and even for new staff) then it useful to have an additional method for checking out the code that does not require SSH access to a server. We have achieved this by creating a PHP script that executes SVN commands in the following way. In order to use a PHP script that can run SVN commands you must ensure that your apache user (normally called ‘www-data’) has sufficient permissions to:

  1. create and edit folders
  2. run SVN commands

Your PHP script should then contain the following:

<?php
svn_checkout('SVN-REPOSITORY-LOCATION', dirname(__FILE__) . 'DESTINATION-FOLDER');
?>

You should replace the following with your own values:

  1. SVN-REPOSITORY-LOCATION – the URL location of the respository you are checking out e.g. http://www.example.com/svnroot/calc/trunk
  2. DESTINATION-FOLDER – the location on the server where the repository should be checked out to (if omitted, the basename of the URL will be used as the destination). e.g. /calc

Useful links:

  1. https://bytenota.com/how-to-checkout-svn-in-php/
  2. https://www.php.net/manual/en/function.svn-checkout.php
  3. https://stackoverflow.com/questions/12178894/svn-checkout-via-php

BrightLemon