Hi all,
I was testing out how to upload a file as per this example
http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/
(BTW it should say CUploadedFile not CUploadFile)
It seems to be doing something but I know not what. Where did the file I “uploaded” go? I’m working on “localhost”.
THe print_r($model) says
"Pic Object ( [image] => CUploadedFile Object ( [_name:private] => diver.jpg [_tempName:private] => C:\PROGRA~1\EASYPH~1.0B1\\tmp\php563.tmp [_type:private] => image/jpeg [_size:private] => 23136 [_error:private] => 0 [_e:private] => [_m:private] => ) [_errors:private] => Array ( ) [_validators:private] => [_scenario:private] => [_e:private] => [_m:private] => ) "
I think it got the filename right, but that’s all I could tell.
My code :
//in the Models/Pic.php
class Pic extends CFormModel
{
public $image;
public function rules()
{
return array('image', 'file', 'types'=>'jpg');
}
}
//in the Controllers/PicController.php
class PicController extends CController
{
public function actionCreate()
{
$model=new Pic;
if(isset($_POST['Pic']))
{
$model->image=$_POST['Pic'];
$model->image=CUploadedFile::getInstance($model,'image');
//if($model->Save())
//CFormModel does not have a Save method? but has a SaveAs?
$model->image->saveAs('forestry1/');
// redirect to success page
echo "success?";
print_r($model);
}
$this->render('create', array('model'=>$model));
}
}
and the View is just:
<?php echo CHtml::form('','post',array('enctype'=>'multipart/form-data')); ?>
...
<?php echo CHtml::activeFileField($model, 'image'); ?>
...
<?php echo CHtml::submitButton('Create', array('submit'=> 'Create')) ; ?>
<?php echo CHtml::endForm(); ?>
The image file "diver.jpg" does not appear in the folder? What happened? What did the code do?