Reputation: 860
I have a pdf file that was submitted from a user (file upload). When I got the file I ran it through two functions and stored it in the database as a blob.
$file = base64_encode(file_get_contents($pathToFile));
What I want to do, is send the pdf back to the browser without writing it to the disk (server side). If I build the file again, saving it to disk, and generate a link to the file then it opens fine.
file_put_contents("pathToPutFile", base64_decode($dataFromDB));
But if I try to send the pdf file to the browser without saving to disk I get an error "Failed to load PDF document" pictured below.
The code used to attempt this feat is below.
$data = base64_decode($origSel['InvestigationFile']);
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="Statement"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
echo $data;
Is sending the file to the user even possible if the file isn't written to disk (server side) first?
If it is possible, what am i missing/doing wrong?
If it isn't possible, what makes it impossible and what would be the best way to ensure that the PDF file gets deleted from the server as soon as the user is done viewing it?
Upvotes: 0
Views: 2066
Reputation: 860
This was failing because I had some output before this part of the document.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div>
"navigation bar goes here"
</div>
<?php
$data = base64_decode($origSel['InvestigationFile']);
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="Statement"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
echo $data;
?>
After removing all the HTML, I was able to get this to work.
Upvotes: 2