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”:
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<title>Upload to Rackspace Cloud Files</title>
-
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
-
</head>
-
<body>
-
<form action="upload.php" enctype="multipart/form-data" method="POST">
-
Username: <input name="username" type="text" /><br />
-
Key: <input name="key" type="password" /><br />
-
Container: <input name="container" type="text" /> <br />
-
File: <input name="upload" type="file" /><br />
-
<input name="submit" type="submit" value="Upload To Rackspace!" />
-
</form>
-
</body>
-
</html>
After that is saved, create a new file called “upload.php” and save the following:
-
<?php
-
// Turn on error reporting so we can see if there are any problems
-
error_reporting(E_ALL);
-
ini_set('display_errors', 1);
-
-
// include the API
-
require('cloudfiles.php');
-
-
// cloud info
-
$username = $_POST['username']; // username
-
$key = $_POST['key']; // api key
-
$container = $_POST['container']; // container name
-
-
ob_start();
-
-
// Print some information regarding who and where
-
echo "Username: $username<br/>";
-
echo "Container: $container<br/>";
-
-
$localfile = $_FILES['upload']['tmp_name'];
-
$filename = $_FILES['upload']['name'];
-
-
// Print some information about the file
-
echo "Local: $localfile<br/>";
-
echo "File : $filename<br/>";
-
-
ob_flush();
-
-
// 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->create_container($container);
-
-
// store file information
-
-
ob_flush();
-
-
// upload file to Rackspace
-
$object = $container->create_object($filename);
-
$object->load_from_filename($localfile);
-
-
$uri = $container->make_public();
-
print "Your URL is: " . $object->public_uri();
-
-
ob_end_flush();
-
?>
The important part is on line 46 that contains “$object->public_uri();”. That is function in the API binding that returns the objects URL.
