hello all…
Thanks before to read my post… i need anything help about saving image.
I did my code below by reading from :
http://www.yiiframework.com/forum/index.php?/topic/3457-issue-with-uploading-to-database/
http://www.yiiframework.com/doc/cookbook/2/
if i have got miss understanding , i wish anyone tell me too…
I have a task to upload an image…
what i have done, i will tell below :
My Model:
Image.php
…
public function rules()
{
return array(
array('', 'safe', 'on'=>'search'),
array('gambar','file',
'types'=>'jpg, gif, png, jpeg',
'maxSize'=>1024 * 1024 * 50,
'tooLarge'=>'The file was larger than 50MB. Please upload a smaller file.',
'allowEmpty'=>true,
),
);
}
…
My Controller : ImageController.php
…
public function actionCreate()
{
$model=new Image;
if(isset($_POST['Image']))
{
$model->attributes=$_POST['Image'];
//$model->gambar = 'E:\Windows_Data\titi\Ling2\centil.jpg';
$img=CUploadedFile::getInstance($model,'gambar');
if($img === null)
echo 'KOSONG TERNYATA'; // Run this Statement ( $img is null )
if(!$img->getHasError())
$model->Path = file_get_contents($img->getTempName());
if($model->save())
{
echo 'Name : '.$img->name;
echo 'Type : '.$img->type;
echo 'Size : '.$img->size;
echo 'Temp Name : '. $img->tempName;
//$model->Path->saveAs($model->Path->name);
$this->redirect(array('view','id'=>$model->ID));
}
}
$this->render('create',array(
'model'=>$model,
));
}
My View : _form.php
<?php $form=$this->beginWidget(‘CActiveForm’, array(
//'enctype'=>'multipart/form-data',
'id'=>'image-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'Title'); ?>
<?php echo $form->textField($model,'Title',array('size'=>60,'maxlength'=>200)); ?>
<?php echo $form->error($model,'Title'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Path'); ?>
<?php echo $form->textField($model,'Path',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'Path'); ?>
</div>
<?php //echo CHtml::form('','post',array('enctype'=>'multipart/form-data')); ?>
<?php echo CHtml::activeFileField($model, 'gambar'); ?>
<?php //echo CHtml::endForm(); ?>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
I very stuck with all of my experiment… can anyone help me to give me a clue or anything , to solve my problem here… I GOT $img STILL NULLL.
What I want to do is : i want to try to save an image into database with column type BLOB ( if there is another better way than what i did here, you can give me an alternatif too )…
help me plz… thanks before for all…
Thanks a lot