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.
Name this file upload.html
Name this file upload.php.
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>
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); ?>
No comments:
Post a Comment