PHP将变量写入客户端下载的文件

I'm attempting to make documents uploaded to my web server a little more secure by encoding them with base64 (in addition to a few other things). My end goal is to have the client click a link and a php script will grab the encoded file, decode it, and then prompt the client to download the decoded file. I can get the file decoded and stored into a variable, but can't seem to get it into something they can download. This is what I have patched together so far but is just outputs the whole mess to the browser without asking to download the file.

$getFile = file_get_contents('myDoc.pdf');
$fileDecode = base64_decode($getFile);

header('Content-Description: File Transfer');
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="finishedFile.pdf"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
echo $fileDecode;

Try setting the content type like so:

http://yogeshchaugule.com/blog/2013/how-display-pdf-browser-php

Also, make sure there's not an empty line at the end of the php file that echos the pdf text.

You will need a little help from JS if you want the user to take an action in the browser. This is probably the most simple way to pull it off but it might be better to implement a call to a totally different php file via ajax and then trigger the download. At any rate:

<?
    // check that the post params have been set
    if(isset($_GET['f'])){
        // get parameter from query string and decode the filename
        $file = base64_decode($_GET['f']);

        // return the file after checking that it exist
        if (file_exists($file)) {
            // load the files contents
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.basename($file));
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            readfile($file);
            exit;
        } else {
            $err = "Oops. That file does not exist.";
        }
    }else{
        // check for download errors
        $err = "false";
    }
?>

<html>
    <head>
        <script>
            var err = "<? echo $err; ?>";
            if(err !== "false"){
                alert(err);
            }

            function download(file){
                if (confirm('You are about to download the "'+atob(file)+'". Would you like to continue?')) {
                    var url = window.location.href.split('?')[0]+"?f="+file;
                    window.location = url;
                } else {
                    // do nothing. they said no.
                }
            }
        </script>
    </head>
    <body>
        <button onclick="download('dGVzdC5wbmc=')">Download File</button>
    </body>
</html>

This assumes the file is called test.png but you can change it to whatever as long as your base 64 is correct.