0

i've tried searching solutions to my problem for about 2 days now and i can't seem to make my code work. My goal is to download a PDF file on a button click but i want the path of the file to be hidden for the users. The project im working on is using Kohana(3.3) framework. I've tried to call that function with ajax :

public function action_download_pdf()
{
        $this->auto_render = false;
        if ($this->request->post()) {

            $data = $this->request->post();
            $file = 'uploads/pdfs_folder/'.$data['pdf_number'].'.pdf';

            if (file_exists($file)) {
                header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename='.$data['pdf_number'].'.pdf');
                header('Content-Transfer-Encoding: binary');
                header('Expires: 0');
                header('Cache-Control: must-revalidate');
                header('Pragma: public');
                header('Content-Length: ' . filesize($file));
                ob_clean();
                flush();
                readfile($file);
                exit;
            }
        }
    }

I'm getting Status Code: 200 OK, but i only get it to show in the "preview" tab on my developer console.

https://i.stack.imgur.com/vyAiK.jpg

I can't figure out what should i do to dowload it instead of showing it that way ?

  • Check please - https://stackoverflow.com/questions/20830309/download-file-using-an-ajax-request – Dmitry Leiko Jan 10 '20 at 12:27
  • Does this answer your question? [How to force a pdf download automatically?](https://stackoverflow.com/questions/2598658/how-to-force-a-pdf-download-automatically) – bluppfisk Jan 10 '20 at 12:29

3 Answers3

0

This has worked for me perfectly.

if ($filename) {

    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=".basename($filename)); 
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$filename");

}
Amit Sharma
  • 1,775
  • 3
  • 11
  • 20
0

below code worked fine for me


<?php
    if(isset($_REQUEST['path']) && $_REQUEST['path'] != "") {

        $file_url = $_REQUEST['path'];
        $pdfname = basename ($file_url);
        header('Content-Type: application/pdf');
        header("Content-Transfer-Encoding: Binary");
        header("Content-disposition: attachment; filename=".$pdfname);
        readfile($file_url);
    }
?>

In your code i noticed

header('Content-Type: application/octet-stream');

The content-type reference in the PHP is important , it's the MIME type of the file you're protecting. If, for example, you saved an MP3 file instead, you'd need to replace application/pdf with audio/mpeg.

I hope this will help you.

Thanks

0

So this is how i solved my issue:

I made a table in my DB in which i generate and save one fake number and one real number. When i reach the step with the button to download the PDF i submit a form thru that button which triggers a function in my Controller. The function is the code i initially posted. I check the fake number in my table and get the real one.