0

How can I refresh the page after force_download in codeigniter controller, here is my controller function. It is not performing any function after calling the force_download.

public function download(){
    $this->load->library('user_agent');
    $this->load->helper('download');
    $file_id = $this->uri->segment(3);

    if(!empty($file_id)){
        //get file info from database
        $fileInfo = $this->Files->get_file($file_id);

        foreach ($fileInfo as $key => $object) {
            $download_direc = $object->file_direc;
        }
        //download file
        force_download($download_direc, NULL);
         //increment the download value by 1 in the db
        $this->Files->update_download($file_id);
        //refresh the page
        redirect($this->agent->referrer());
    }
}

Thank you

Roman
  • 183
  • 3
  • 16
  • redirect($_SERVER['REQUEST_URI'], 'refresh'); – Devsi Odedra May 28 '18 at 09:24
  • Not working,it seems can not redirect any more after performing `force_download` – Roman May 28 '18 at 09:29
  • 1
    Possible duplicate of [PHP generate file for download then redirect](https://stackoverflow.com/questions/822707/php-generate-file-for-download-then-redirect) – Pradeep May 28 '18 at 09:46
  • You shouldn't even have to do this as you just want to go back to the initial page where the download button was clicked (e.g. the button that links to the download method). Basically the browser realizes its a "file" and treats it as any other download. So you should stay on the same page. Remove the redirect. Do you not see this happening as I've described? – Alex May 29 '18 at 04:36

2 Answers2

1

you can try this. i hope it will help you.

public function download($fileName = NULL) {   
   if ($fileName) {
    $file = realpath ( "download" ) . "\\" . $fileName; 
    if (file_exists ( $file )) {
     $data = file_get_contents ( $file );

     force_download ( $fileName, $data );
    } else {

     redirect ( base_url () );
    }
   }
  }
PHP Geek
  • 3,949
  • 1
  • 16
  • 32
1

On the view page when user clicks your anchor link or button to download the file. Just take the id of that button and put a jQuery interval to 2500-3000 so the page will refresh after 2.5-3 seconds.

BlackXero
  • 880
  • 4
  • 17