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
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.
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.
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.