I would like to upload an image and save the image name in database. I can create it and everything is ok, but update has problems. If i update the $model->img (image) value will be blank in db.
This is my model rules:
array('img', 'file','types'=>'jpg, gif, png, jpeg', 'allowEmpty'=>true, 'on'=>'update'),
array('title, img', 'length', 'max'=>255, 'on'=>'insert,update'),
And this is the controller:
public function actionUpdate($id)
{
$model=$this->loadModel($id);
$_SESSION['KCFINDER']['disabled'] = false;
$_SESSION['KCFINDER']['uploadURL'] = Yii::app()->baseUrl."/../images/"; // URL for the uploads folder
$_SESSION['KCFINDER']['uploadDir'] = Yii::app()->basePath."/../../images/"; // path to the uploads folder
// $this->performAjaxValidation($model);
if(isset($_POST['News']))
{
$_POST['News']['img'] = $model->img;
$model->attributes=$_POST['News'];
$model->img = $_POST['News']['img'];
$uploadedFile=CUploadedFile::getInstance($model,'img');
if($model->save()){
if(!empty($uploadedFile))
{
$uploadedFile->saveAs(Yii::app()->basePath.'/../../images/'.$model->img);
}
}
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
Before the save i check $model->img and it has value.
So i think something wrong with save() in update.
$_POST['News']['img'] = $model->img;
Why you did it? You must remove this line.
$model->attributes=$_POST['News'];
$model->img = $_POST['News']['img'];
Related
I want to upload a image and change the original name then save it.
Model:
public function rules()
{
return array(
array('image', 'file', 'types'=>'jpg, gif, png'),
);
}
Controller:
$model->image = CUploadedFile::getInstanceByName('image');
If i save it without any other actions it will work.
But how could i change the name of image ? I try something like below:
$model->image->name = "xxx"; //CUploadedFile.name readonly
if($model->save())
$model->images->saveAs(some_path_else.newname); //the files's new name is different from database
$model->image = "abc.jpg"; //wont save it
Is the image attribute must be an instance of CUploadedFile?
Anyone help pls
do something like this
$uploadedFile = CUploadedFile::getInstance($model, 'image');
if (!empty($uploadedFile)) {
//new name will go here
$model->image = strtotime($this->getCurrentDateTime()) . '-' .$uploadedFile;
}
//this will save the image with new name
$uploadedFile->saveAs(Yii::app()->basePath.'/../public/images/user/' . $model->image);
Thanks for your answer.
I figured it out.
CUploadedFile.name is read only , so i can't change it.
The Model need a new public attribute :
public $file;
public function rules()
{
return array(
array('file', 'file', 'types'=>'jpg, gif, png'),
array('iamge', 'length'=>'255'),
);
}
Then in the controller:
$model->file = CUploadedFile::getInstanceByName('image');
$model->file->saveAs(Yii::app()->basePath.'/../public/images/user/' . $model->image);
$model->image = $model->file->name;
It's works fine.(it's not real code)
see here
This is my code
can everyone help me please? i want my system insert new record when i update it in yii framework
public function actionUpdate($id)<br/>
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Entrydata']))
{
$_POST['Entrydata']['photo'] = $model->photo;
$model->attributes=$_POST['Entrydata'];
$uploadedFile = CUploadedFile::getInstance($model,'photo');
if($model->save())
{
if(!empty($uploadedFile)) // check if uploaded file is set or not
{
$uploadedFile->saveAs(Yii::app()->basePath.'/../images/'.$model->photo); // image will uplode to rootDirectory/banner/
}
$this->redirect(array('view','id'=>$model->id_entry));
}
}
$this->render('create',array(
'model'=>$model,
));
}
Unset attribute before save.
$model->unsetAttributes(array("id_entry"));
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Entrydata']))
{
$_POST['Entrydata']['photo'] = $model->photo;
$model->attributes=$_POST['Entrydata'];
$uploadedFile = CUploadedFile::getInstance($model,'photo');
// Unset Primary Key value.
$model->unsetAttributes(array("id_entry"));
if($model->save())
{
if(!empty($uploadedFile)) // check if uploaded file is set or not
{
$uploadedFile->saveAs(Yii::app()->basePath.'/../images/'.$model->photo); // image will uplode to rootDirectory/banner/
}
$this->redirect(array('view','id'=>$model->id_entry));
}
}
$this->render('create',array(
'model'=>$model,
));
}
I try to upload a file to MySQL table and it does not work.
here what i write:
view:
<div class="row">
<?php echo $form->labelEx($model,'doc_ordered_recieved'); ?>
<?php echo $form->fileField($model,'doc_ordered_recieved'); ?>
<?php echo $form->error($model,'doc_ordered_recieved'); ?>
</div>
model:
i add this attribute:
public $doc_ordered_recieved;
and this rulse:
array('doc_ordered_recieved','file','types'=>'pdf', 'allowEmpty'=>true, 'on'=>'update'),
controllers:
public function actionCreate()
{
$model=new Orders;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Orders']))
{
$model->attributes=$_POST['Orders'];
$model->doc_ordered_recieved=CUploadedFile::getInstance($model,'doc_ordered_recieved');
if($model->save())
{
$doc_ordered_recieved->saveAs('http://localhost/files');
$this->redirect(array('view','id'=>$model->oid));
}
}
$this->render('create',array('model'=>$model,
));
}
please help me i don't know why its not work????
thanks you all
eliya
First you need to change the rule to add create scenario:
array('doc_ordered_recieved','file','types'=>'pdf', 'allowEmpty'=>true, 'on'=>'insert,update'),
and in your create action from your controller you need to do this:
public function actionCreate()
{
$model=new Orders;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Orders']))
{
$model->attributes=$_POST['Orders'];
$uploadedFile = CUploadedFile::getInstance($model,'doc_ordered_recieved');
if($model->save())
{
if(!empty($uploadedFile)) // check if uploaded file is set or not
{
if($model->image == null || empty($model->image)){
$rnd = rand(0,9999);// generate random number between 0-9999
$fileName = "{$rnd}-{$uploadedFile}";
$model->image = $fileName;
}
$uploadedFile->saveAs(dirname(__FILE__)..'/files/'. $model->doc_ordered_recieved);
// redirect to success page
}
$this->redirect(array('view','id'=>$model->oid));
}
}
$this->render('create',array('model'=>$model,
));
}
Just by looking at your code I can see that you need to change your file path from
$doc_ordered_recieved->saveAs('http://localhost/files');
to
$doc_ordered_recieved->saveAs(Yii::app()->basePath.'path/to/localFile');
Also, you should provide more information about your model.
I am quite new to php yiiframework. I have users model where it takes users basic information including profile image but i have to delete a image file when I updates new image for my profile.
I uploaded file by using following method.
public function actionUpdate()
{
$model=$this->loadModel($id);
$random = substr(number_format(time() * rand(),0,'',''),0,10);
if(isset($_POST['Users']))
{
$model->attributes=$_POST['Users'];
$uploadedFile=CUploadedFile::getInstance($model,'image_path');
$fileName = $milliseconds. '-' .$random;
$model->image_path = $fileName;
if($model->save())
$uploadedFile->saveAs(Yii::app()->basePath.'/../images/uploaded/'.$fileName);
$this->redirect(array('view','id'=>$model->id));
}
$data = PanXCore::getDataForCreateUser();
$userRoles = new UserRoles();
$this->render('create',array(
'model'=>$model,
'roles' => $data['roles'],
'userRoles' => $userRoles
));
}
use unlink() for example
unlink(Yii::app()->basePath.'/../images/uploaded/'. $oldfile);
I'm implementing some file uploading fields and i'm having some difficulties with it.
I was following this article and everything was perfect until I tried to update my model.
If I didn't fill the file field, it was cleared after save. I was googling and find this topic. I've modified my code, but now, when I try to modify another field, it isn't save, unless I modify the image field too.
Where's the problem?
My model code:
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Company']))
{
$model->attributes=$_POST['Company'];
$the_image = CUploadedFile::getInstance($model,'image');
if (is_object($the_image) && get_class($the_image)==='CUploadedFile')
$model->image = $the_image;
if($model->save())
if (is_object($the_image))
$model->image->saveAs(Yii::getPathOfAlias('webroot') . '/upload/' . $model->image);
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
}
And these are the rules in my model:
array('name', 'required'),
array('type, number, rating, user_id', 'numerical', 'integerOnly'=>true),
array('name, image, address, site, mail', 'length', 'max'=>1000),
array('text', 'safe'),
array('image', 'file', 'types'=>'jpg, gif, png'),
array('image', 'unsafe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, type, name, text, address, site, mail, number, rating, user_id', 'safe', 'on'=>'search'),
Please, help me. I have no clue why it isn't working.
Try modifying your image attribute rule on the Update scenario like this:
array('image', 'file', 'types'=>'jpg, gif, png','allowEmpty' => true, 'on'=>'update'),
I found this tip here, hope it works for you.
public function uploadMultifile ($model,$attr,$path)
{
if($sfile=CUploadedFile::getInstances($model, $attr)){
foreach ($sfile as $i=>$file){
$formatName=time().$i.'.'.$file->getExtensionName();
$file->saveAs(Yii::app()->basePath .DIRECTORY_SEPARATOR.'..'. $path.$formatName);
// $model->image->saveAs(Yii::app()->basePath . '/../studentimage/' . $date . $model->image);
$ffile[$i]=$formatName;
}
return ($ffile);
}
}
public function actionCreate()
{
$model=new Subject;
// $model->date_erected = date('Y-m-d');
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Subject']))
{
$model->attributes=$_POST['Subject'];
if($filez=$this->uploadMultifile($model,'imagea','/../images/views/'))
{
$model->documents=implode(",", $filez);
}
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}