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.

  • Daudu_ade

    im getting file not found after using this method exactly the way u have stated

    • File not found where exactly?

      • Daudu_ade

        ok let me give you a quick look into my code so you see what i mean.
        require(‘/cloud/cloudfile/cloudfiles.php’);
        require(‘/upgradephp/upgrade.php’);
        // die(‘im here’);
        // cloud info
        $username = “something”; // 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 = ‘Daudu_Test_Container’;//
        $container = $conn->create_container($container);
        //$container = $conn->get_container($container);

        // store file information
        $localfile = $_FILES[‘upload’][‘tmp_name’];
        $filename = $_FILES[‘upload’][‘name’];

        $filetype = getFileMimeType($localfile);//get the file
        //echo $filetype;
        //echo mime_content_type($localfile);
        //die();
        // upload file to Rackspace
        $object = $container->create_object($filename);
        $object->content_type=$filetype;//set the content type
        $object->load_from_filename($localfile);
        $public_uri = $container->make_public();
        die(‘file has been uploaded to: ‘.$public_uri);
        everything went well except at the end when i tried to get the file then i get the file not found error.

        • My apologies that I haven’t had the chance to reply sooner. Looking at your code, everything looks correct. Where does the “getFileMimeType” function get defined though?

  • rcollazo thanks for taking the time to write this article. We’ve been looking for a php based solution that would allow us to upload multiple files at once but cant seem to find one that ties into the rs cloud file api directly. Have you come across anything that would cover the above? We’ve got single uploads going no problem but want to expand upon it.

    • Sorry for the delay in my response! I’d probably have to know more to understand what exactly you’re looking for. Will you please email me directly so we can discuss? You can reach me at robert [at] collazo dot ws.