Yii Framework Forum: User Profile Pic - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

User Profile Pic how to upload user profile photo? Rate Topic: -----

#1 User is offline   YasserHossam 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 21-August 13

Posted 21 August 2013 - 02:56 PM

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
0

#2 User is offline   jkofsky 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 536
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 21 August 2013 - 03:08 PM

View PostYasserHossam, on 21 August 2013 - 02:56 PM, said:

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.
Do, or do not. There is no 'try.' Jedi Master Yoda
1

#3 User is offline   Alexandre Rodichevski 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 15-August 13
  • Location:Italy

Posted 21 August 2013 - 05:03 PM

Hi YasserHossam,
see the discussion.
1

#4 User is offline   YasserHossam 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 21-August 13

Posted 22 August 2013 - 03:29 PM

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
0

#5 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 22 August 2013 - 08:21 PM

Going to plug my own extension here and suggest you try Yush - http://www.yiiframew...extension/yush/

If you post your controller and model code, I'll be happy to assist you to get it working.

Thanks,

Matt
1

#6 User is offline   YasserHossam 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 21-August 13

Posted 23 August 2013 - 01:59 AM

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
0

#7 User is offline   Ankit Modi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 896
  • Joined: 19-February 13
  • Location:India

Posted 23 August 2013 - 05:02 AM

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"));
    }
		
?>

Thanks,
Ankit Modi
Skype : amodi06
1

#8 User is offline   YasserHossam 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 21-August 13

Posted 23 August 2013 - 09:07 AM

@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
0

#9 User is offline   jkofsky 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 536
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 23 August 2013 - 03:06 PM

View PostYasserHossam, on 22 August 2013 - 03:29 PM, said:

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)),...)

Do, or do not. There is no 'try.' Jedi Master Yoda
0

#10 User is offline   YasserHossam 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 21-August 13

Posted 24 August 2013 - 03:19 AM

@jkofsky ,

its worked finally

thank you very much for your help
0

#11 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 24 August 2013 - 05:49 AM

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()}";


Cheers,

Matt
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users