Wednesday, December 13, 2017

php - download in the browsers, not as a download

In making a band chart/pdf management system, I needed a method for streaming/download files.

PHP makes it pretty easy, but I had to bang around just a bit before I could figure out how to let it try and open stuff in a browser window (vs going to straight to the download folder)

The code ended up a s
if(securityCheck(getLoggedInUser(),$_BAND,false)){
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: '.mime_content_type($file)); 
            #vs application/octet-stream or application/pdf
    header('Content-Disposition: inline; filename="'.basename($file).'"');
            #vs Content-Disposition: attachment
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);
} else {
print "NO LUCK $FILENAME";
}
   exit;
} else {
print "UNAUTHORIZED";
exit;
}

The bits that were different from the original example I copy and pasted are in bold.

Also, I learned the power of urldecode() :-D

No comments:

Post a Comment