Hello
i am new in Yii development
i made a model for the users and its working very fine but i want to make the user upload his photo and i show it in the view
i made "BLOB" field in my database for the photo
how can i save it in the db ? and how to show it in the view ? and what i need to change in the controll?
Thanks,
Yasser Hossam
jkofsky
(Jkofsky)
August 21, 2013, 8:08pm
2
YasserHossam:
Hello
i am new in Yii development
i made a model for the users and its working very fine but i want to make the user upload his photo and i show it in the view
i made "BLOB" field in my database for the photo
how can i save it in the db ? and how to show it in the view ? and what i need to change in the controll?
Thanks,
Yasser Hossam
Maybe instead of a blob field, you could use a varchar(). In the upload process just save the file to a directory like ‘images/avatars’. Then in the display just set an <img/> tag with the image.
I have seen info on the various sites about how to store an image in a blob field, but I have did it.
Thank you very much jkofsky ,Alexandre Rodichevski
raelly helped me alot.
now i can upload the photo and it uploaded to the directory and with the name wich i specified .
but i can’t show it in the view.php or _view.php
i made a lot of effort to try that but i feel there is some problem , the image not appear in any case
some of code wich i used
<?php echo CHtml::image(Yii::getPathOfAlias(‘webroot’).’/protected/UsersPhotos/’.$model->Photo); ?>
<?php echo CHtml::image(dirname(Yii::app()->getBasePath()).DIRECTORY_SEPARATOR.‘protected’.DIRECTORY_SEPARATOR.‘UsersPhotos’.DIRECTORY_SEPARATOR.‘defualt.jpg’, ‘DORE’); ?>
<?php
echo "<img src=\"";
echo CHtml::encode('..\..\UsersPhotos/'.$model->Photo);
echo "\" alt=\"image\" height=\"200\" width=\"20\"/></img>";
?>
<?php echo CHtml::image(dirname(Yii::app()->getBasePath()).DIRECTORY_SEPARATOR.‘protected’.DIRECTORY_SEPARATOR.‘UsersPhotos’.DIRECTORY_SEPARATOR.‘defualt.jpg’, ‘DORE’); ?>
Note: i am sure that it is the right path but the image not displayed in any of them !
and i made html file and put img tag with the same path and it desplayed normally
please advide me .
Thanks,
Yasser Hossam
Going to plug my own extension here and suggest you try Yush - http://www.yiiframework.com/extension/yush/
If you post your controller and model code, I’ll be happy to assist you to get it working.
Thanks,
Matt
Hello Matt,
Really thank you .
i don’t know the matter need to use extension or not . i just need to make the image appear like in html .
here is my controller creation function
public function actionCreate()
{
$model=new Users;
if(isset($_POST['Users']))
{
$model->attributes=$_POST['Users'];
$UserName=$model->UserName;
$uploadedFile=CUploadedFile::getInstance($model,'Photo');
$fileName = "{$UserName}_{$uploadedFile}";
$model->Photo = $fileName;
if($model->save())
{
$uploadedFile->saveAs(Yii::getPathOfAlias('webroot').'/protected/UsersPhotos/'.$fileName); // image
// redirect to success page
$this->redirect(array('view','id'=>$model->UserID));
}
}
$this->render('create',array(
'model'=>$model,
));
}
my view :
<b><?php echo CHtml::encode($model->getAttributeLabel('Photo')); ?>:</b>
<?php echo CHtml::encode (dirname(Yii::app()->getBasePath()).DIRECTORY_SEPARATOR.'protected'.DIRECTORY_SEPARATOR.'UsersPhotos'.DIRECTORY_SEPARATOR.'defualt.jpg', 'DORE'); //show the path ?>
<?php
echo "<img src=\"";
echo CHtml::encode('..\..\UsersPhotos/'.$model->Photo);
echo "\" alt=\"image\" height=\"200\" width=\"20\"/></img>";
?>
thanks again
Ankit_Modi
(Ankit Modi)
August 23, 2013, 10:02am
7
if you want to show the image on view file
for e.g
1) in _form.php you can write a
'htmlOptions'=>array('enctype' => 'multipart/form-data');
2) In your controller image filed save on db
$model->image = CUploadedFile::getInstance($model, 'BLOB');
$model->image->saveAs('upload/coupon/'.$model->BLOB);
3)
Display the image on _form.php file
<?php
$baseUrl = Yii::app()->baseUrl;
$img = $baseUrl.'/upload/banner/'.$model->BLOB;
$img1 = $baseUrl.'/upload/banner/no_images.jpg';
if($img!='' && file_exists(Yii::app()->request->baseUrl.'/upload/banner/').$img) {
echo CHtml::image($img,'',array("height"=>"3 0ox","width"=>"30px"));
}else {
echo CHtml::image($img1,'',array("height"=>"30ox","width"=>"30px"));
}
?>
@Ankit Modi ,Thanks brother
the problem may that i was uploading the images in folder in the protected folder
now it is in a folder in the project root
and i used some of your code and it is working very well
thank you very much
jkofsky
(Jkofsky)
August 23, 2013, 8:06pm
9
YasserHossam:
Thank you very much jkofsky ,Alexandre Rodichevski
raelly helped me alot.
now i can upload the photo and it uploaded to the directory and with the name wich i specified .
but i can’t show it in the view.php or _view.php
You need to make the ‘UserPhotos’ available to the web. The ‘protected’ directory is NOT accesssable via a browser. I would change the save location to ‘/images/UserPhotos’ then use:
try:
<?php echo CHtml::image(Yii::app()->baseUrl.'/images/UsersPhotos/'.$model->Photo); ?>
else, you could leave the avators in /protected/UsersPhotos, then create an action in the controller to get the avator
public function actionGetAvator($id) {
//find the correct avator from $User->...
//send it as an image file by using the php header() and sendFile() methods.
}
To generate the link something like this:
<?php echo CHtml::link(CHtml::image(array('site/getavator','id'=>$data->id)),...)
@jkofsky ,
its worked finally
thank you very much for your help
If you ever did want to use the Yush extension, you could use this code.
public function actionCreate()
{
$model=new Users;
if(isset($_POST['Users']))
{
$model->attributes=$_POST['Users'];
$UserName=$model->UserName;
$uploadedFile=CUploadedFile::getInstance($model,'Photo');
$fileName = "{$UserName}_{$uploadedFile->getName()}";
$model->Photo = $fileName;
if($model->save())
{
$fullPath = Yush::getPath($model, Yush::SIZE_ORIGINAL, $model->Photo);
$uploadedFile->saveAs($fullPath);
$this->redirect(array('view','id'=>$model->UserID));
}
}
$this->render('create',array(
'model'=>$model,
));
}
// View
<b><?php echo CHtml::encode($model->getAttributeLabel('Photo')); ?>:</b>
<img src="<?php echo Yush::getUrl($model, Yush::SIZE_ORIGINAL, $model->Photo); ?>" alt="image" height="" width=""/>;
Also, you line of code
$fileName = "{$UserName}_{$uploadedFile}";
should probably be
$fileName = "{$UserName}_{$uploadedFile->getName()}";
[size="2"]Cheers,[/size]
[size="2"]Matt[/size]