- Step 1: Download the Files
- Step 2: Create the Upload File Form
- Step 3: Edit the Create Action in the Controller
** 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.
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
if (isset($_POST['Model'])){ // Where Model = your model
if($model->save()){
// 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:
$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)
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.