Image Cannot Be Displayed

Hi all, I am facing problem of displaying image on view file. I have successfully uploaded image path to database and saved it in (webroot)images folder. I have no idea how to display the photo out since there is no error is returned. Anyone can help me? Any solution to overcome this problem? Thanks in advance.

Here’s my code:



public function actionCreate()


		$model=new Student;

		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);



			$rnd = rand(0,9999);  // generate random number between 0-9999




            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name

            $model->foto = $fileName;





		$uploadedFile->saveAs(Yii::getPathOfAlias('webroot') .'/images/'.$fileName);











public function rules()


		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('username, password, programme, intake, email, contact', 'required'),

			array('contact', 'numerical', 'integerOnly'=>true),

			array('username', 'length', 'max'=>10),

			array('password, programme, intake', 'length', 'max'=>30),

			array('email', 'length', 'max'=>50),

			array('foto', 'length', 'max'=>255, 'on'=>'insert,update'),

			array('foto', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'),

			// The following rule is used by search().

			// @todo Please remove those attributes that should not be searched.

			array('id, username, password, programme, intake, email, contact', 'safe', 'on'=>'search'),





<h1>View Student #<?php echo $model->id; ?></h1>

<?php echo CHtml::Image(Yii::getPathOfAlias('webroot').'/images/'.$model->foto); 

<br/> <br/>

<?php $this->widget('zii.widgets.CDetailView', array(











)); ?>



<?php $form=$this->beginWidget('CActiveForm', array(



	'htmlOptions' => array(

        'enctype' => 'multipart/form-data', //tambahan dari _form hasil gii


)); ?>

	<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,'foto'); ?>

        <?php echo CHtml::activeFileField($model, 'foto'); ?>  // by this we can upload image

        <?php echo $form->error($model,'foto'); ?>


    <?php if($model->isNewRecord!='1'){ ?>

    <div class="row">

     <?php echo CHtml::image(Yii::app()->request->baseUrl.'/upload/'.$model->foto,"foto",array("width"=>200)); ?>  // Image shown here if page is update page


	 <?php } ?>


	<div class="row">

		<?php echo $form->labelEx($model,'username'); ?>

		<?php echo $form->textField($model,'username',array('size'=>10,'maxlength'=>10)); ?>

		<?php echo $form->error($model,'username'); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'password'); ?>

		<?php echo $form->passwordField($model,'password',array('size'=>20,'maxlength'=>20)); ?>

		<?php echo $form->error($model,'password'); ?>



Dear all, I can solve it by using Yii::app()->basePath instead of Yii::getPathOfAlias(‘webroot’). It works perfectly :lol:

You should be using Yii::app()->request->baseUrl (or the shortcut Yii::app()->baseUrl) because you need a URL rather than a path. You’ve used this correctly in your _form view, so I’m not sure how this confusion has arisen.

Ya, you are right! Thanks for the comment.