0

I have the following code to download the pdf file after clicking the hyperlink for the pdf file, now I would like to change the action to open the pdf file in browsing new tab instead of downloading the file to local, could you help to advise how to update it?

public function downloadFile($fileID)
{
    $this->load->model('Mod_UploadFile');
    $this->load->helper('download');   
    $result = $this->Mod_UploadFile->getFileByID($fileID);
    if($result)
        force_download($result['FILE_NAME'], $result['UPLOAD_FILE']->load(), true);
    else
        echo "404 Not Found!";
}
zidniryi
  • 1,212
  • 3
  • 15
  • 36
Ken
  • 9
  • 2

2 Answers2

0

You can redirect your users from php using:

header('www.example.com/path/to/pdf/aaa.pdf');
die();

make sure to enter the correct path to your pdf here or alternatively use a relative path like demonstrated here: How do I make a redirect in PHP?

0

the header will not help you to open pdf in a new tab. It will open in the current tab. You need to write -

header('/files/pdf_name.pdf');

If you want to open the pdf in a new tab better use JavaScript like this in PHP -

<?php
if(isset($_POST['view_pdf'])){

echo "<script type=\"text/javascript\">
        window.open('/files/pdf_name.pdf', '_blank')
    </script>";

}

?>
Tamzid Ahmed
  • 576
  • 4
  • 12