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:

controller:




....

public function actionCreate()

	{

		$model=new Student;


		// Uncomment the following line if AJAX validation is needed

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


		if(isset($_POST['Student']))

		{

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

            $model->attributes=$_POST['Student'];

 

            $uploadedFile=CUploadedFile::getInstance($model,'foto');

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

            $model->foto = $fileName;

 

            if($model->save())

            {

         

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

                $this->redirect(array('view','id'=>$model->id));

            }

        }


		$this->render('create',array(

			'model'=>$model,

		));

	}

...



model:




....

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

		);

	}

....



view:




<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(

	'data'=>$model,

	'attributes'=>array(

		'id',

		'username',

		'password',

		'programme',

		'intake',

		'email',

		'contact',

	),

)); ?>




_form:




...

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

	'id'=>'student-form',

	'enableAjaxValidation'=>false,

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

     </div>

    <?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

     </div>

	 <?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>


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

	</div>

....



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.