To start with let me give you a brief insight on RackSpace Cloud files.
First up
First of all in my opinion Rackspace Cloud is far better option than Amazon’s offering when it comes to pricing, black listed IP addresses and general speed of your transactions with your Cloud.
Cloud Files provides you and your applications unlimited online storage for media – items like backups, video files, or user content. And in an industry first, you can deliver that content to your users at blazing speeds over the market-leading global CDN platform, powered by Akamai Technologies, Inc.
Benefits
- Scalable, dynamic storage. Use as much or little as you want and only pay for what you use.
- Manage files through their well written and usable online control panel or pro-grammatically through their REST API.
- Last but not the least: Serve files publicly at blazing speeds through Akamai’s CDN technology.
Now said that First thing first you need to join Rackspace Files. Here is the link to join RS Cloud files
Its good to know that Cloud Files is tied directly into Akamai’s EdgePlatform so all the uploaded content can be distributed to one of the world’s largest CDN platforms comprised of 84,000+ secure servers deployed in over 72 countries within nearly 1,000 networks.
Now the best part
Pricing
- Storage starts at $0.15 per gigabyte.
- Outgoing bandwidth starts at $0.18 per gigabyte
- Pay only for what you use.
So that now it seems like an interesting bet and you have made up your mind Lets
. The reason I am so supportive of Rackspace CF is because of the easy to use API and blazing fast speed when it comes to downloading your content such as images. And I like this line on their websiteNo contracts. No setup fees. Cancel at any time
Now if you have all setup by now its time to roll. Download Cloudfiles API from here.
How do you install the downloaded API:
- Extract the downloaded archive and make sure the source code files are in your PHP “include path”.
- To use the API in your source code, just make sure to include/require the “cloudfiles.php” script.
Pre-requisite for the above API to work are listed below
- This API requires PHP version 5.0+
- PHP’s cURL module
- PHP enabled with mbstring (multi-byte string) support. If you don’t have this installed on Linux machine then run this command as super user
#>yum install php-mbstring
You can find out if this module is install or not by looking at the list that is produced by this command
#>php -m
Or just check in phpinfo() - PEAR FileInfo module (for Content-Type detection). This extension is enabled by default as of PHP 5.3.0.
Now I assume that all the above prerequisites are met with and that you are ready to rule
. First thing I would like to tell is that there is no way that the file that you are trying to upload can be pushed directly to Rackspace cloud. It has to come to your server first but you need not to save it on your server.
I now assume that you have joind RS cloud files and you have your RS Cloud Files API key with you.
There are several uploaders available free of cost. Flash based, HTML5 based and so on. but lets keep things simple and do this with a simple HTML file upload form.
Below is the code for HTML form. Name this file as uploader.php and let it sit in your current site document root.
<form action="uploader.php" method="POST" enctype="multipart/form-data">Select a File: <input type="file" name="to_be_uploaded_file" /> <input type="submit" name="submit" value="Send to Rackspace Cloud Files!" /> </form>
I am not gonna do any validation for “onFormSubmit” (pseudo) so I assume that all the validations such as file size requirement and file extensions will be handled by you.
Now in your uploader.php file add these lines to the top of the file
// include the Rackspace Cloud files PHP API require('cloudfiles.php'); $rackspace_user = "rschahaljaspreet"; // replace this with your rackspace user name $rackspace_api_key = "wow7d180b26bmysecapikeyf80**"; // In case you dont remember this you can get it under my account // Lets connect to Rackspace $authentication = new CF_Authentication($rackspace_user, $rackspace_api_key); $authentication->authenticate(); $connection = null; try { $connection = new CF_Connection($authentication); } catch(AuthenticationException $e) { echo "Unable to authenticate ".$e->getMessage(); } $container = null; // create a new container if you like uncomment the line below // $container = new CF_Container($authentication,$connection,"testcontainer"); // Or use an already exsting container // $container = $connection->get_container('testcontainer'); // or better way to handle this according to me is try { $container = $connection->get_container('images'); $container->make_public(); } catch(NoSuchContainerException $e) { $container = new CF_Container($authentication,$connection,"images"); } catch(InvalidResponseException $res) { // let your users know or try again or just store the file locally and try again later to push it to the Cloud } // store file information $file_to_be_uploaded = $_FILES['to_be_uploaded_file']['tmp_name']; $filename = $_FILES['to_be_uploaded_file']['name']; // upload this file to Rackspace CF servers $object = $container->create_object($filename); $object->load_from_filename($file_to_be_uploaded); echo $object->public_uri(); // thats it you are done. }
You can store the path returned by public_uri() function in your database.
I just ran a quick test on the code above and was able to upload a file without any hiccups
here is the URL of my uploaded file
http://c284900.r0.cf1.rackcdn.com/iRecommend.jpg
Now if you notice that the URL c284900.r0.cf1.rackcdn.com is something that is provided by Rackspace. What if you want your own website URL for instead of one supplied by RS.
Simply create a CNAME record such as images.jaspreetchahal.org and point it to your RS CDN container URL.
So I did the same and below is my custom URL for the above
http://images.jaspreetchahal.org/iRecommend.jpg
I hope this helps. If it does I wouldn’t mind you leaving a comment for me
Please add your experiences using the comments section below.
Download full source from here
Next up is to use the same technique but using plupload. That will be one of my next articles.
Cheers
how to: upload files to Rackspace Cloud Files using PHP API and get the public URL for the uploaded file.,





JC WordPress Coupon Revealer Plugin Pro License
Australian Street Names with City, State and Display Names only, Single Server License
Hi Jaspreet,
It looks like the PHP library you reference in this awesome tutorial has been deprecated. Any chance you can post another one using this: http://php-opencloud.com/ ?
Thanks!
I guess its been a while I haven’t done anything with RS, thanks for sharing that link, I shall look into that soon.
Cheers
Yes, Good example. Enough to start.
I want to contribute the following:
When you have (pseudo) subdirectories/subfolders in your cointaines trying getting subdirs with “get_container()” will fail due “Container name cannot contain a ‘/’ character.” I mean this will fail dramatically:
$container = $connection->get_container(urlencode(‘container/subdir’));
===
So the way to upload to subdirectories is at the “create_object” stage, like this:
$object = $container->create_object(‘subdir/’ . $filename);
$object->load_from_filename($file_to_be_uploaded);
NomikOS recently posted..CheckGmail Error: 200 OK – Solución definitiva.
Twitter: agassmann
says:
thanks for this example, better that rackspace documentation examples
Thanks for this… this really help me out..
A heads up for those of us using the UK CloudFiles servers, you need to go to line 137 in cloudfiles.php and change $auth_host=US_AUTHURL to $auth_host=UK_AUTHURL or you’ll get a 401 resulting in a failure to authenticate.
Hey Jaspreet, thanks for the reply! That would be awesome if you could share a bit of your code with us, I’m still having issues trying to use plupload to upload multiple files my CF account.
-Brian
Hey Jaspreet, great post. I was wondering if you ever got around to working on a plupload + rackspace cloud file implementation guide?
Thanks!
-Brian
Hi Brian, Thanks for you feedback. Much appreciated. With regard to plupload, you still have to receive the file onto the server before uploading to RackSpace files. If you are interested then I can do a write up. I’ve written code that I use for one of my projects, with some modification I should be able to share that. Let me know if you want me to share that.
Cheers!
Bookmarked! Thank you for being detailed..
best tutorial for using the cloud files php api out there, thanks! this contained 2 things i was looking for, make_public and the uri of the uploaded file.
I am glad that it helped you
Cheers!