Uploading files to Rackspace Cloud (Mosso) using PHP API Comments (2)
by Wez Pyke - November 30, 2009 in PHP, Text Tutorials

Rackspace Cloud is a great VPS service, but when I recently done a project it needed the functionality of uploading directly from a website to the cloud. I did not find many tutorials on how to do this, so I thought I'd write a tutorial on it myself.

Step 1: Download the required files

The files you will need for this are located at the Rackspace Cloud website here. You can download the API for PHP, Java, Python, .NET and Ruby. But for this tutorial we will be using PHP.

Step 2: Setup

When the download of the PHP API has complete decompress the file and rename it to cloudfiles. Now place the folder named cloudfiles in the directory where the PHP files will be stored that will upload to the Rackspace Cloud.

Step 3: Create an upload form

For this tutorial we will only be creating a basic upload form, but I have planned for a more advanced uploader with a progress bar and multiple file uploads for a future tutorial, so be sure to check back for that.

<form action="upload.php" enctype="multipart/form-data" method="POST">
File: <input name="upload" type="file" /> 
 
<input name="submit" type="submit" value="Upload To Rackspace!" />
</form>

Name this file upload.html

Step 4: Uploading to Rackspace Cloud

So what we need to do to upload to the Rackspace cloud is to include the PHP API, connect to Rackspace, get the container we want to use, create an object and then upload the file to that object.

<?php
// include the API
require('cloudfiles/cloudfiles.php');
 
// cloud info
$username = ""; // username
$key = ""; // api key
 
// Connect to Rackspace
$auth = new CF_Authentication($username, $key);
$auth->authenticate();
$conn = new CF_Connection($auth);
 
// Get the container we want to use
$container = $conn->get_container('ContainerName');
 
// store file information
$localfile = $_FILES['upload']['tmp_name'];
$filename = $_FILES['upload']['name'];
 
// upload file to Rackspace
$object = $container->create_object($filename);
$object->load_from_filename($localfile);
?>

Name this file upload.php.

Download

You can download the source files from here

Post a comment

Please post a comment and let us know if you have anything that you'd like to add to the tutorial or if you are having any problems.

Bookmark and Share
Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • E-mail this story to a friend!
  • FriendFeed
  • LinkedIn
  • Ping.fm
  • Tumblr
  • Twitter

Related posts:

  1. PHP Class for Google Maps API A few days ago I was developing a website for...
  2. PHP Contact Form Every website needs a way of contacting the people behind...
  3. Send form data to PHP without the whole page reloading using jQuery This is my screencast for the Nettuts/Screenr screencast competition. I...
  4. Build a Twitter-like site with CodeIgniter and jQuery Part 2 Don't forget to check out part 1 if you...
  5. Adding security to CodeIgniter forms with a custom library class The class that we are going to create within CI...

Related posts brought to you by Yet Another Related Posts Plugin.

Your Ad Here

2 Responses to “Uploading files to Rackspace Cloud (Mosso) using PHP API”

  1. Lynks says:

    Hi, I have been asked to develop an uploader for a site. The uploader should store files uploaded from end user machines to the rackspace cloud. It is important that the webserver hosting the uploader keeps minimal traffic, so using your script will the file go straight from the end user to the cloud, or will it go through the webserver hosting the upload.php?

    Thanks.

  2. Wez says:

    Well it needs to go through the server because it has to be stored temporarily to then be transferred to the cloud, it's impossible to go straight from the end user to the cloud without the server in the middle.

Leave a Reply