0

I have to develop a profile page which has user image shown and an update button to change the profile picture. To show image and update button in view looks like below

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
    <?= Html::img($imgPath,['width'=>100,'height'=>100]);?>
    <?= $form->field($model, 'imageFile')->fileInput(); ?>
    <?= Html::submitButton('Update Profile Picture',['class'=>'btn btn-primary','id'=>'uploadButton']) ?>
    <?php ActiveForm::end() ?>

With fileInput I am choosing new file and saving it on server with below line

$model->imageFile->saveAs('uploads/profilepictures/' . $session['user_name'] . '_profimg.' . $model->imageFile->extension);

The image is successfully getting uploaded to server (overwriting the old image) but when I refresh my web page the old image is displayed even though it is replaced in server with new one. Html::img is still fetching the older image not sure why (and from where??). Someone please help!

  • Did you clear your browser cache (CTRL+F5 in Chrome)? – Aleksandr Shumilov Jun 16 '15 at 02:12
  • It worked after clearing the cache! Thanks! But I am building a webpage (user profile) and when user uploads the image and doesn't see the new image he/she will be confused. Is there any way I can avoid this? – karan ratnaparkhi Jun 16 '15 at 02:20
  • Yes, just add some hash after the image path. You better look at http://stackoverflow.com/questions/728616/disable-cache-for-some-images. But keep in mind, that you do need caching, as loading image every single time will kill all the user experience from your site. Plus you will have extra bandwidth usage. – Aleksandr Shumilov Jun 16 '15 at 02:23
  • Also, you can paly around the headers - this way is described as the second reply to the link I've provided to you. – Aleksandr Shumilov Jun 16 '15 at 02:25
  • Thanks a lot! It worked added ?dummy=randomstring after img src path. – karan ratnaparkhi Jun 16 '15 at 02:48
  • Good! Anyway, be carefull with it and remember about end-users and bills from your network provider:) – Aleksandr Shumilov Jun 16 '15 at 02:54

0 Answers0