Uploading Files to Rackspace Cloud using PHP (Aka Mosso)
3 Apr
Uploading Files to Rackspace Cloud using PHP (Aka Mosso)
Uploading files directly from a PHP script to Rackspace Cloud is actually easier than you might think although there are not many tutorials for this kind of script, so we thought we’d share the information. We have also included the source code for this tutorial.
- First you will need a Rackspace Cloud Account, if you don’t have one, sign up here.
- Once you have a Rackspace Cloud Account you will need to download the Rackspace Cloud API, these are available here. For this tutorial you will need to download the PHP API.
- Extract and upload the folder and rename it to something sensible, we will rename it to “cloudapi”.
- Next we need a simple upload script, written in HTML:
<form action="upload.php" enctype="multipart/form-data" method="POST"> File: <input name="uploadfile" type="file" /> <input name="submit" type="submit" value="Upload" /> </form>
- Now we have the upload script we need the upload.php code:
<?php
// include the Cloud API.
require('cloudapi/cloudfiles.php');
//Required username and API key found in your account
$username = ""; //Username
$key = ""; //API key
//Connecting to Rackspace Cloud
$auth = new CF_Authentication($username, $key);
$auth->authenticate();
$conn = new CF_Connection($auth);
//Set the Container you want to use
$container = $conn->get_container('INSERTCONTAINERNAME');
//Temp store the file
$localfile = $_FILES['uploadfile']['tmp_name'];
$filename = $_FILES['uploadfile']['name'];
//Uploading to Rackspace Cloud
$object = $container->create_object($filename);
$object->load_from_filename($localfile);
//Completion Message
echo "Your file has been uploaded";
?>
- We will now run through the above code;
- Line 03- ensure this file exists and the path is correct.
- Line 06 & 07 – You will need to enter your Rackspace Cloud API key, this can be found in your account at Your Account -> API Access.
- Line 15 – Insert your container name here, case may matter, so keep and eye out for your Capital Letters.
Download the Source Code Here.
Getting errors? Keep an eye out for our new blog post over the next couple of days where we will discuss errors and issues we had when trying to run this script and how you can solve them.
Edit
Tip:
For those folks with UK Rackspace accounts, you’ll need to use the following call when creating the $auth object:
$auth = new CF_Authentication($username, $key, NULL, UK_AUTHURL);
(as of v.1.7.9)
The reason being is that there are two different authentication URL’s for each client base.
Thanks to Anh for this tip.







