Uploading files
Handling file uploads is a pretty common task for a web application. Yii has some helpful classes built in to do this. Let's create a simple form that will allow uploading ZIP archives and storing them in protected/uploads.
Getting ready
Before starting with this recipe, perform the following:
Create a fresh application using
yiic webapp.In your protected directory, create an
uploadsdirectory.
How to do it...
We will start with the model, so create
protected/models/Upload.phpas follows:<?php class Upload extends CFormModel { public $file; public function rules() { return array( array('file', 'file', 'types'=>'zip'), ); } }Now we will move on to the controller, so create
protected/controllers/UploadController.php:<?php class UploadController extends Controller { function actionIndex() { $dir = Yii::getPathOfAlias('application.uploads'); $uploaded = false; $model=new Upload(); if(isset($_POST['Upload...