shravya
(Shravyav1991)
February 24, 2014, 4:29am
1
Hi,
I have been trying to upload a file to database since 10days but failing each time.
I have tried 2methods:
upload the actual file to DB.
store the path only.
I am pretty sure that the second method should be preferred, I have gone thru many forums but could not find any.
Any help would be of great use to me.
shravya:
Hi,
I have been trying to upload a file to database since 10days but failing each time.
I have tried 2methods:
upload the actual file to DB.
store the path only.
I am pretty sure that the second method should be preferred, I have gone thru many forums but could not find any.
Any help would be of great use to me.
Hi shravya.
I have just posted today the solution to second method: "store the path only", please see this message in the next post Image Upload Using Ajax.
I hope this will help you.
bagwari65
(Manish)
February 24, 2014, 1:29pm
3
hi shrav,ya, its better to save path in DB, and sotre file in some folder insiide your Web app.
your model will be like this…
class Item extends CActiveRecord { public $image; // ... other attributes public function rules()
{
return array(
array('image', 'file', 'types'=>'jpg, gif, png'),
);
} }
try your controller like…
class ItemController extends CController {
public function actionCreate()
{
$model=new Item; if(isset($_POST['Item']))
{ $model->attributes=$_POST['Item']; $model->image=CUploadedFile::getInstance($model,'image');
if($model->save())
{ $model->image->saveAs('path/to/localFile'); // redirect to success page
} }
$this->render('create', array('model'=>$model));
}
and finally view file like…
$form = $this->beginWidget( 'CActiveForm',
array( 'id' => 'upload-form', 'enableAjaxValidation' => false, 'htmlOptions' => array('enctype' => 'multipart/form-data'), ) ); // ... echo $form->labelEx($model, 'image');
echo $form->fileField($model, 'image');
echo $form->error($model, 'image');
// ... echo CHtml::submitButton('Submit'); $this->endWidget();
I hope it will help you
shravya
(Shravyav1991)
February 25, 2014, 8:55am
4
Hi,
I have tried the above method, but when I upload the file and say create, it says that the field is empty.
this is my code–
model:
array(‘name’, ‘file’, ‘types’=>‘jpg, gif, png’),
view:
<?php
$form = $this->beginWidget('CActiveForm',
array(
'id' => 'upload-form',
'enableAjaxValidation' => false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)
);
echo $form->fileField($model, 'name');
echo $form->error($model, 'name');
?>
<?php $this->endWidget(); ?>
controller:
public function actionCreate()
{
$model=new Event;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);// AJAX VALIDATION
if(isset($_POST['Event']))
{
$model->attributes=$_POST['Event'];
$model->name=CUploadedFile::getInstance($model,'name');
//$model->name=CUploadedFile::getInstance($model,'name');
if($model->save())
{
//$uploadedFile->saveAs(Yii::app()->basePath.'/../event/'.$fileName);
$model->name->saveAs('//attachments/');
$this->redirect(array('view','id'=>$model->ename));
}
}
$this->render('create',array(
'model'=>$model,
));
}
I have noticed that my view file has a widget in a widget. Would that create a problem?
shravya
(Shravyav1991)
February 25, 2014, 10:29am
5
Hi all,
I have got the solution…there was some problem with nested widgets…
but I’m unable to give the path in the controller.
I gave something like this which is not working. PLz help
model->name->saveAs('.//attachements/);
ani
(Aneesh)
February 26, 2014, 11:12am
6
Try this
$uploadfile= $model->name;
$uploadfile->saveAs(dirname(Yii::app()->basePath) . '/attachments/'.$uploadfile);