Uploading files to Rackspace Cloud (Mosso) using PHP API

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
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • FriendFeed
  • LinkedIn
  • Ping.fm
  • Tumblr
  • Twitter

No related posts.

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

, , , , ,

12 Responses to Uploading files to Rackspace Cloud (Mosso) using PHP API

  1. Lynks December 12, 2009 at 4:17 pm #

    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 December 13, 2009 at 10:56 am #

    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.

  3. M.M.H.Masud February 20, 2010 at 11:25 am #

    i do everything as you say. But i am having this following error:

    Fatal error: Uncaught exception 'BadContentTypeException' with message 'Required Content-Type not set' in C:\xampp\htdocs\rackspace\cloudfiles.php:1645 Stack trace: #0 C:\xampp\htdocs\rackspace\cloudfiles.php(1962): CF_Object->_guess_content_type('C:\xampp\tmp\ph...') #1 C:\xampp\htdocs\rackspace\upload.php(24): CF_Object->load_from_filename('C:\xampp\tmp\ph...') #2 {main} thrown in C:\xampp\htdocs\rackspace\cloudfiles.php on line 1645

    can you please tell me what the problem is ?

    thanks

  4. Aaron February 24, 2010 at 5:30 am #

    Thank you so much! This is a great tutorial! If you end up doing the 'advanced uploader tutorial' I'd be very interested! Thanks again!

  5. Milo March 17, 2010 at 2:32 pm #

    Hi there
    I am getting the following error when I try to use the form:
    Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in /var/www/vhosts/universeoffaith.com/httpdocs/v_upload_RSCloud/cloudfiles/cloudfiles_http.php on line 209

    Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in /var/www/vhosts/universeoffaith.com/httpdocs/v_upload_RSCloud/cloudfiles/cloudfiles_http.php on line 1140

    Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in /var/www/vhosts/universeoffaith.com/httpdocs/v_upload_RSCloud/cloudfiles/cloudfiles_http.php on line 1140

    Any thoughts?

    Thanks

    Milo

  6. Milo March 17, 2010 at 4:23 pm #

    I resolved my issue.
    Thanks
    M.

  7. Milo March 18, 2010 at 1:56 am #

    Hi Wez
    I set the php upload_max_filesize to 400mb but i get an error when i upload a 90mb file.
    Any thoughts?

    Regards

    Marcelo

  8. Wez Pyke March 18, 2010 at 10:29 am #

    @Milo I'm not quite sure why it is returning an error when you upload a 90mb file, sorry. You could ask the developers on Twitter @CloudFusion.

    I hope you can resolve your problem.

  9. joshb June 21, 2010 at 11:30 pm #

    well i'm sure Milo solved his problem but I thought it might be worth mentioning. Not only is there a upload_max_filesize but there is also an upload max for form posts. (exact variable name I don't remember) That variable also has to be set large enough to allow a large file.

  10. Rifat July 30, 2010 at 3:54 am #

    Hi,

    I am getting following errors while uploading large files (25MB, 43MB, 87MB):

    Notice: Undefined index: upload in C:\Program Files\Apache Group\Apache\htdocs\virtualhosts\www.mysite.com\tools\admin\cloudup.php on line 23

    Notice: Undefined index: upload in C:\Program Files\Apache Group\Apache\htdocs\virtualhosts\www.mysite.com\tools\admin\cloudup.php on line 24

    Notice: Undefined index: upload in C:\Program Files\Apache Group\Apache\htdocs\virtualhosts\www.mysite.com\tools\admin\cloudup.php on line 29

    Notice: Undefined index: upload in C:\Program Files\Apache Group\Apache\htdocs\virtualhosts\www.mysite.com\tools\admin\cloudup.php on line 30

    Fatal error: Uncaught exception 'IOException' with message 'Could not open file for reading: ' in C:\Program Files\Apache Group\Apache\htdocs\virtualhosts\www.mysite.com\tools\admin\includes\cloudfiles\cloudfiles.php:2007 Stack trace: #0 C:\Program Files\Apache Group\Apache\htdocs\virtualhosts\www.mysite.com\tools\admin\cloudup.php(32): CF_Object->load_from_filename(NULL) #1 {main} thrown in C:\Program Files\Apache Group\Apache\htdocs\virtualhosts\www.mysite.com\tools\admin\includes\cloudfiles\cloudfiles.php on line 2007

    These errors show up after around 10-15 minutes of starting upload, it seems to process the request till that time. It works fine when files are smaller in size. I have added the following lines in the beginning of the upload script:

    set_time_limit(0);
    ini_set("upload_max_filesize", "1000M");
    ini_set("post_max_size", "1001M");
    ini_set("memory_limit", "1002M");
    ini_set("max_input_time", "7200");

    What might be the problem? Any thoughts?

  11. Curtis September 13, 2010 at 10:05 pm #

    Hi Wez,

    I'm on RackSpace Cloud too and have had issues uploading very large files as Milo does... I believe my issue is either the tmp folder on their PHP cluster fills up or their load balancer's limits the script to 30 secs. Either way I can't change those settings.

    They have suggested that I use Cloud files to solve this issue but if what you say is true then I would have the same problems.

    Have you uploaded very large files to cloud files via the PHP API?

  12. Brian Roscher December 13, 2010 at 2:03 pm #

    Using a tweak of your script, I was able to upload files to the Rackspace Cloud. Great... now how do I download them?

Leave a Reply