Uploading to Rackspace Cloud Files with PHP

Someone recently asked me if I knew of a web based uploader that returned the Akamai CDN URL of the file. I looked around and found this site but it wasn’t quite what was wanted. I decided to edit the code in the example from that site and came up with this.

First, you’ll need the PHP bindings for Rackspace Cloud Files. They can be found here. You’ll need to put the files cloudfiles.php, cloudfiles_http.php, and cloudfiles_exceptions.php in directory on your web server.

Next, save the following code as “index.html”:

“index.html”
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Upload to Rackspace Cloud Files</title>
  5. <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
  6. </head>
  7. <body>
  8. <form action="upload.php" enctype="multipart/form-data" method="POST">
  9.     Username: <input name="username" type="text" /><br />
  10.     Key: <input name="key" type="password" /><br />
  11.     Container: <input name="container" type="text" /> <br />
  12.     File: <input name="upload" type="file" /><br />
  13.     <input name="submit" type="submit" value="Upload To Rackspace!" />
  14. </form>
  15. </body>
  16. </html>

After that is saved, create a new file called “upload.php” and save the following:

“upload.php”
  1. <?php
  2. // Turn on error reporting so we can see if there are any problems
  3. error_reporting(E_ALL);
  4. ini_set('display_errors', 1);
  5.  
  6. // include the API
  7. require('cloudfiles.php');
  8.  
  9. // cloud info
  10. $username = $_POST['username']; // username
  11. $key = $_POST['key']; // api key
  12. $container = $_POST['container']; // container name
  13.  
  14. ob_start();
  15.  
  16. // Print some information regarding who and where
  17. echo "Username: $username<br/>";
  18. echo "Container: $container<br/>";
  19.  
  20. $localfile = $_FILES['upload']['tmp_name'];
  21. $filename = $_FILES['upload']['name'];
  22.  
  23. // Print some information about the file
  24. echo "Local: $localfile<br/>";
  25. echo "File : $filename<br/>";
  26.  
  27. ob_flush();
  28.  
  29. // Connect to Rackspace
  30. $auth = new CF_Authentication($username, $key);
  31. $auth->authenticate();
  32. $conn = new CF_Connection($auth);
  33.  
  34. // Get the container we want to use
  35. $container = $conn->create_container($container);
  36.  
  37. // store file information
  38.  
  39. ob_flush();
  40.  
  41. // upload file to Rackspace
  42. $object = $container->create_object($filename);
  43. $object->load_from_filename($localfile);
  44.  
  45. $uri = $container->make_public();
  46. print "Your URL is: " . $object->public_uri();
  47.  
  48. ob_end_flush();
  49. ?>

The important part is on line 46 that contains “$object->public_uri();”. That is function in the API binding that returns the objects URL.

Thumbs DownThumbs Up (+1 rating, 1 votes)
Loading ... Loading ...