I’m working with the tutorial posted about file uploads: http://www.yiiframework.com/doc/cookbook/2/
I have the code working that I placed into actionCreate(), however, I have to duplicate that code in actionUpdate()
I tried making another method above those two to prevent code duplication called: paperUpload(), but I have no idea how to call it using Yii (or if I’m even doing this correctly)
Here is the code:
public function paperUpload()
{
$model=new Paper;
$file=CUploadedFile::getInstance($model, 'filename');
$model->image->saveAs('C:/Users/chuntley/ElementDesign/www/dev/files/test.jpg');
}
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model=new Paper;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Paper']))
{
$model->attributes=$_POST['Paper'];
if($model->save())
{
paperUpload();
$this->redirect(array('view','id'=>$model->papid));
}
}
$this->render('create',array(
'model'=>$model,
));
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
*/
public function actionUpdate()
{
$model=$this->loadModel();
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Paper']))
{
$model->attributes=$_POST['Paper'];
if($model->save())
{
paperUpload();
$this->redirect(array('view','id'=>$model->papid));
}
}
$this->render('update',array(
'model'=>$model,
));
}