Yii 1.1: Upload Files to Rackspace Cloud Files CDN


** I actually made this into an extension for easier use. You can view the extension: http://www.yiiframework.com/extension/rackspaceconnect/

Step 1: Download the Files

The first thing that you will need to do is download and unzip the Rackspace php Cloudfiles folder onto your server. I installed it on MYDOMAIN.com/protected/lib .

The location of the folder: https://github.com/rackspace/php-cloudfiles/tree

Step 2: Create the Upload File Form

This step has already been detailed in previous tutorials itself. You can view a good one here.


Step 3: Edit the Create Action in the Controller

You will need to insert the following into the create action after

if (isset($_POST['Model'])){ // Where Model = your model
// include the API
// cloud info
$username = ""; // username
$key = ""; // api key
// Connect to Rackspace
$auth = new CF_Authentication($username, $key);
$conn = new CF_Connection($auth);
// Get the container we want to use
$container = $conn->get_container('ContainerName');
// store file information
$model->file=CUploadedFile::getInstance($model,'file'); // file = name you put in model
$filename = $model->file->getName();
// upload file to Rackspace
$object = $container->create_object($filename);

Remove If You Don't Need to Upload to Own Server:


A few Notes:

  • You'll need to add your rackspace username and key
  • You'll need to insert your container name
  • You'll need to modify your view files to grab uploaded items from correct location (i.e img src)

Be the first person to leave a comment

Please to leave your comment.

Write new article