I have a task to create company profile of company where i work. Then i decide using Yii framework to develop that. One of the fitur is news and it needs upload image to complete the news. I write in here just because i can open it again in the future. i realize that this way to upload is simpler than php non framework. here we go :
in view -> form, we have :
$form = $this->beginWidget('CActiveForm', array(
'id' => 'news-form',
'enableAjaxValidation' => false,
'htmlOptions' => array('enctype' => 'multipart/form-data')
));
?>
labelEx($model, 'newsImage'); ?>
error($model, 'newsImage'); ?>
endWidget(); ?>
in models, we have :
class News extends CActiveRecord
{
public $newsImage;
//other function
public function rules()
{
return array(
array('newsId', 'numerical', 'integerOnly'=>true),
array('newsTitle, newsAuthor', 'length', 'max'=>200),
array('newsImage', 'file', 'types'=>'jpg, gif, png'),
array('user_in, user_up', 'length', 'max'=>8),
array('newsDate, newsContent, date_in, date_up', 'safe'),
array('newsId, newsTitle, newsDate, newsContent, newsImage, newsAuthor, user_in, date_in, user_up, date_up', 'safe', 'on'=>'search'),
);
}
//other function
}
in controller we have save create example :
public function actionCreate() {
$model = new News;
if (isset($_POST['News'])) {
$model->attributes = $_POST['News'];
$model->newsImage = CUploadedFile::getInstance($model, 'newsImage');
if ($model->save()) {
$model->newsImage->saveAs(Yii::app()->basePath.'/../images/news/'.$model->newsImage);
$this->redirect(array('view', 'id' => $model->newsId));
}
}
$this->render('create', array(
'model' => $model,
));
}
I hope this help. I get reference from here :
http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/
in view -> form, we have :
$form = $this->beginWidget('CActiveForm', array(
'id' => 'news-form',
'enableAjaxValidation' => false,
'htmlOptions' => array('enctype' => 'multipart/form-data')
));
?>
labelEx($model, 'newsImage'); ?>
error($model, 'newsImage'); ?>
endWidget(); ?>
in models, we have :
class News extends CActiveRecord
{
public $newsImage;
//other function
public function rules()
{
return array(
array('newsId', 'numerical', 'integerOnly'=>true),
array('newsTitle, newsAuthor', 'length', 'max'=>200),
array('newsImage', 'file', 'types'=>'jpg, gif, png'),
array('user_in, user_up', 'length', 'max'=>8),
array('newsDate, newsContent, date_in, date_up', 'safe'),
array('newsId, newsTitle, newsDate, newsContent, newsImage, newsAuthor, user_in, date_in, user_up, date_up', 'safe', 'on'=>'search'),
);
}
//other function
}
in controller we have save create example :
public function actionCreate() {
$model = new News;
if (isset($_POST['News'])) {
$model->attributes = $_POST['News'];
$model->newsImage = CUploadedFile::getInstance($model, 'newsImage');
if ($model->save()) {
$model->newsImage->saveAs(Yii::app()->basePath.'/../images/news/'.$model->newsImage);
$this->redirect(array('view', 'id' => $model->newsId));
}
}
$this->render('create', array(
'model' => $model,
));
}
I hope this help. I get reference from here :
http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/
7 komentar:
nitin rawat said...
how will you call the individual image on view page
arum said...
try this one :
"< style="float:left; margin: 10px 10px 10px 0px;" src="request->baseUrl; ?>/images/news/newsImage); ?>" height="80" width="100" />"
I use html code.
arum said...
before "style" word is "img"
nitin rawat said...
image file name should be with respect to the id number.
by using your syntax it is showing error. as it is not getting the file name.
Here, the file uploaded should name w.r.t the id number. so that we can call the specific id image only.
kindly let me know how we can do this..
i am newbie to yii.. plz help. Thanks in advance
nitin rawat said...
i have managed to save file by id like 1.jpg, 2.jpg, etc (w.r.t "id")
and also able to call on view page by
""
but here as you can see.. .jpg extension is added manually
so it will only work for .jpg files
we need to rename the whole file name during uploading (assigned by framework) and by this we call it by the same name on view page.
Thanks in advance.
nitin rawat said...
"< img src="basePath.'?> /../images/id.'.jpg' ?>" height="400" >"
arum said...
When I store the picture, i don't rename the file (add ".jpg"). By default, it will store as the original name, for example : fish.jpg.
You can save the path of file too if you dont wanna type the path.