Avoid browser caching with Codeigniter

Facing chache browser related issue.

function doUpload(){ $data['includeView'] = "profileconfirm"; $config['upload_path'] = './img/images/uploaded/'; $config['allowed_types'] = 'gif|jpg|png|jpeg'; $config['max_size'] = '5000'; $config['max_width'] = '1024'; $config['max_height'] = '768'; $config['file_ext'] =".jpeg"; $config['file_name'] = $profileId.$config['file_ext']; $config['overwrite'] = TRUE; $this->load->library('upload', $config); $query = null ; if ( ! $this->upload->do_upload()){ // Error here }else{ // Image uploaded sucess fully // $profile - business logic to populate $profile $data['PROFILE_DETAILS'] = $profile; $this->load->view('index', $data); } 

This method is used to upload images. After successfully loading the image, it loads the index view page, which inside includes the profile view page.

But on the profileconfirm page, the new uploaded image will not be displayed. Sometimes it works fine, but sometimes it doesn't, it happens in most cases.

Please, help

+4
source share
3 answers

You can send the correct headers to the client to disable the cache:

 .... $this->output->set_header("HTTP/1.0 200 OK"); $this->output->set_header("HTTP/1.1 200 OK"); $this->output->set_header('Last-Modified: '.gmdate('D, d MYH:i:s', $last_update).' GMT'); $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate"); $this->output->set_header("Cache-Control: post-check=0, pre-check=0"); $this->output->set_header("Pragma: no-cache"); $this->load->view('index', $data); 

Note: the output class is initialized automatically.

+13
source

Just add the timestamp to the src attribute of the displayed image.

 <img src="filename.jpg?<?php echo time(); ?>"> 

To completely disable the cache with a single line of code (after expanding the output library), see http://www.robertmullaney.com/2011/08/13/disable-browser-cache-easily-with-codeigniter/
Disclaimer, my blog

Edit 1: the decision made, in my opinion, is too large when all you want to do is reload the image in the browser;)
Edit 2: simplify the proposed solution.

+4
source

Try the following:

 if (!$this->upload->do_upload()) { $error = array('errors' => $this->upload->display_errors("<li>","</li>")); $this->load->view('index', $error); }else{ $data['PROFILE_DETAILS'] = $profile; $this->load->view('index', $data); } 

and then display the errors in your view as follows:

 <?php if($errors): ?> <ul><?php print $errors ?></ul> <?php endif; ?> 

and see what errors you get.

-1
source

Source: https://habr.com/ru/post/1336777/


All Articles