Difference between #2 and #3 of Upload Files to Rackspace Cloud Files CDN

unchanged
Title
Upload Files to Rackspace Cloud Files CDN
unchanged
Category
Tutorials
unchanged
Tags
rackspace, CDN, File Upload
changed
Content
_** I actually made this into an extension for easier use. You can view the
extension:
[http://www.yiiframework.com/extension/rackspaceconnect/](http://www.yiiframework.com/extension/rackspaceconnect/
"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](https://github.com/rackspace/php-cloudfiles/tree
"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.

[http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/](http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/
"http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/")


Step 3: Edit the Create Action in the Controller
------------------------------------------------
You will need to insert the following into the create action after



~~~
[php]
if (isset($_POST['Model'])){ // Where Model = your model
        if($model->save()){
~~~




~~~
[php]
// include the API
require(Yii::app()->getBasePath().'/PATH/TO/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
$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);
$object->load_from_filename($localfile);
~~~

**Remove If You Don't Need to Upload to Own Server:**
~~~
[php]
$model->file->saveAs($target_path);
~~~


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)