简体   繁体   中英

How do I get php to open a pdf file in a new tab?

I don't understand the documentation I've been reading. Can anyone here explain how to do this?

I have a HTML link that goes to a function showFile(). There are two gets, an id, which is the file name, and an ext, the extension of the file.(Only extensions will be pdf, jpg, and gif) I'm using codeigniter framwework btw.

I read stuff about headers but when i tried that it just downloaded the file. Any help would be appreciated.

Function so far ---------

 public function showFile () {
    $fileId = $this->input->get('id');
    $fileExt = $this->input->get('ext');

}

Something like this in HTML:

<a href="downloadfile.php?filesrc=blah.pdf" target=_new>
    Click here to download blah.pdf</a>

Where of course the href has to be echo ed in PHP.

<a href="downloadfile.php?filesrc=<?php echo $filepath ?>" target=_new> ... </a>

Oh, and the downloadfile.php will simply have a header('...'); redirect to the file.

class Files extends CI_Controller{

    public function show_file($id, $ext){
       $file_location = '';
       switch($ext){
           case 'pdf':
             $file_location = 'location-to_pdf_files'; // store as constant maybe inside index.php - PDF = 'uploads/pdf/';

             //must have PDF viewer installed in browser !
          $this->output
           ->set_content_type('application/pdf')
           ->set_output(file_get_contents($file_location . '/' . $id . '.pdf'));

           break;
           //jpg gif etc here...
       }

    }
}

-

$route['view/(:any)/(:any)'] = 'files/show_file/$1/$2';

-

<a href="<?php echo site_url('view/picture/pdf');?>" rel="nofollow" target="_blank">Some_file<a/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM