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.


Problems? You can see our troubleshooting guide of problems that we had getting the script to work here.
Hi,
I was trying to make use of your tutorial and I ran into a fatal error which was regarding my container name.. What really is the container?
The container name is the name you have given to the ‘folder’ on rackspace. I believe this is case sensitive.
I get this error: Fatal error: Uncaught exception ‘InvalidResponseException’ with message ‘Unexpected response (): ‘ in /home/public_html/cloudapi/cloudfiles.php:212 Stack trace: #0 /home/public_html/upload.php(11): CF_Authentication->authenticate() #1 {main} thrown in /home/public_html/cloudapi/cloudfiles.php on line 212
Hello Mike,
Ensure your API key is correct. Seems you have an Authentication issue.
Apparently it has to do with Curl Open SSL not being installed, will try that.
i got this error after running this code
Call to undefined function curl_init()
i am getting error after running this code is
Call to undefined function curl_init()
Not shown above, but 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.
Thank you for your answer Anh.
I will add your post to the original post.
sorry… newbie question. are we running a cron on the upload.php file? and will the index file be on the root of the site or actually inside the cloudapi folder.. reason I ask is if its on the root, php site doesnt show.
Thanks
@Timothy – Your index.php file can be anywhere although you need to make sure you reference your cloudapi file to the correct directory.