Echo the contents of the file to the user, but make it act like a download

I have a script where I get the contents of the file and then echo it to the screen, the problem is that it actually echos the binary file to the page, what I want, if for it to act as a download, the download dialog will be displayed.

how can i achieve this

+3
source share
4 answers

From the PHP header () manual :


// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');

Change the content type and file name. You can read the user file through get_contents_file, but either work or work.

+5
source

, Content-disposition. . . .

Content-type application/octet-stream, .

(: application/binary, , , , application/octet-stream, " Content-Type , application/octet-stream [...], - . , - ". : Google).

+2

Content-Type HTTP.

"" PHP (.. - ) :

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

script, - .

+1

Thanks @Brad for the answer. I made some changes to it. What I found is that my content is repeated, not "read file". I have done it.

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="download.pdf"');
$pdf = $thirdPartyAPI->getPdf($ID);
echo $pdf;
ob_clean();
flush();
exit;
0
source

Source: https://habr.com/ru/post/1756795/


All Articles