2011-01-24 3 views
4

브라우저 chache와 관련된 직면 한 문제.Codeigniter를 사용하여 브라우저 캐시를 피하는 방법

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

이 방법은 이미지 업로드에 사용됩니다. 이미지 업로드가 성공적으로 완료되면 내부적으로 profileconfirm보기 페이지가 포함 된 인덱스보기 페이지가로드됩니다.

프로필 확인 페이지에서 업로드 된 새 이미지가 반영되지 않습니다. 때로는 잘 작동하지만 때로는 그렇지 않은 경우가 대부분입니다.

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

을 다음과 같이보기에 오류를 표시 :

답변

13

당신은 캐시를 비활성화하기 위해 클라이언트에 적절한 헤더를 보낼 수 있습니다

.... 
$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 M Y H: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); 

참고 : 출력 클래스는 자동으로

+1

'$ last_update '는 무엇입니까? –

+0

왜 $ last_update를 사용하고 있습니까? – sheetal

-1

다음을 시도 도와주세요

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

을하고 점점 어떤 오류를 참조하십시오.

+0

오류가 발생하지 않습니다. 내 문제는 chache와 관련이 있습니다. 파일은 서버 폴더에 성공적으로 업로드되었지만 새로 업로드 된 파일은 확인 페이지에 반영되지 않습니다. img 태그 – Vicky

+0

@Vicky 이는 이미지와 페이지가 독립적으로 캐시되기 때문입니다. 템플릿에서 이미지 캐싱을 방지하는 방법은 위의 솔루션을 참조하십시오.) –

4

그냥 표시되는 이미지의 src 속성에 타임 스탬프를 추가 초기화됩니다.

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

내 블로그, 리뷰 http://www.robertmullaney.com/2011/08/13/disable-browser-cache-easily-with-codeigniter/
면책 조항 (출력 라이브러리를 확장 한 후) 완전히 한 줄의 코드로 캐시를 사용하지 않으려면

편집 1 : 허용되는 솔루션은 내 의견으로는 과잉이다 모두하고 싶을 때 브라우저에서 이미지를 강제로 다시로드하십시오.)
편집 2 : 제안 된 솔루션을 단순화했습니다.

관련 문제