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

2019-08-16 15:14发布

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

}

2条回答
Animai°情兽
2楼-- · 2019-08-16 15:39

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 echoed 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.

查看更多
家丑人穷心不美
3楼-- · 2019-08-16 15:53
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/>
查看更多
登录 后发表回答