Upload a directory to Rackspace Cloudfiles

October 8, 2009

I put a little piece of sample code together that uploads a directory to Cloudfiles. I plan on running this on my web servers to upload the backup script to a safe place. This will replace my current pull method, which works fine, but costs me plenty in slicehost bandwidth charges.

Obviously, you will want to include php-cloudfiles and set $username, $key, $container and $location as desired. Note: This currently doesn’t do any recursive uploads. I’m sure that will come with time but it’s not something I needed for this application; I am creating tarballs to upload.

$username = 'username';  //Your rackspace cloud username
$key = '';  //Your rackspace cloud API key
$container = 'backup-'.date('Y-m-d'); //Any string you want.  I choose to make mine change daily
$location = '/var/www'; //The path you want to backup

//Create Connection
$auth = new CF_Authentication($username, $key);
$auth->authenticate();
$conn = new CF_Connection($auth);

//Create the container
$cont = $conn->create_container($container);

$handle = dir($location);
while($file = $handle->read())
{
	// upload files:
	if((! is_dir($handle->path."/".$file)))
	{
		echo "Uploading:".($obj = $cont->create_object($file))."\n";
		$obj->load_from_filename($handle->path."/".$file);
	}
}

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive