Uploading Files to Rackspace Cloud using PHP (Aka Mosso)

3 Apr

Rackspace Cloud Logo

Rackspace Cloud Logo

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.

    1. First you will need a Rackspace Cloud Account, if you don’t have one, sign up here.
    2. 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.
    3. Extract and upload the folder and rename it to something sensible, we will rename it to “cloudapi”.
    4. 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>

    1. 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";
?>
  1. 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.

Bookmark and Share

13 Responses to “Uploading Files to Rackspace Cloud using PHP (Aka Mosso)”

  1. Clloud 04. Apr, 2010 at 8:07 pm #

    Problems? You can see our troubleshooting guide of problems that we had getting the script to work here.

  2. Louis Stephens 12. Jul, 2010 at 7:57 pm #

    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?

  3. Clloud 12. Jul, 2010 at 10:58 pm #

    The container name is the name you have given to the ‘folder’ on rackspace. I believe this is case sensitive.

  4. Mike 20. Apr, 2011 at 11:32 am #

    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

  5. Clloud 20. Apr, 2011 at 1:11 pm #

    Hello Mike,

    Ensure your API key is correct. Seems you have an Authentication issue.

  6. Mike 20. Apr, 2011 at 2:18 pm #

    Apparently it has to do with Curl Open SSL not being installed, will try that.

  7. omprakash 14. Jun, 2011 at 11:12 am #

    i got this error after running this code

    Call to undefined function curl_init()

  8. omprakash 14. Jun, 2011 at 11:14 am #

    i am getting error after running this code is
    Call to undefined function curl_init()

  9. Anh 14. Jun, 2011 at 1:04 pm #

    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.

  10. Clloud 14. Jun, 2011 at 9:30 pm #

    Thank you for your answer Anh.

    I will add your post to the original post.

  11. Timothy 28. Sep, 2011 at 10:19 pm #

    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

  12. Clloud 23. Dec, 2011 at 7:50 am #

    @Timothy – Your index.php file can be anywhere although you need to make sure you reference your cloudapi file to the correct directory.

Trackbacks/Pingbacks

  1. Rackspace Cloud Upload with PHP API – Troubleshooting | Clloud – Cloud Hosting Tips and Reviews - 04. Apr, 2010

    [...] may have seen our previous blog post here for uploading files to Rackspace Cloud using the PHP API. Despite the relatively easy script it [...]

Leave a Reply

Connect with Facebook