Home > Concepts and Solutions > Solutions > Browser Downloading Script Instead of File
Browser Downloading Script Instead of File
User Rating: / 2
PoorBest 
Solutions
Written by Philip L Yuson   

Symptom 

  • Click on a document and instead of downloading the document, the browser is downloading the PHP script. When you save it, the document is saved, but the document name is the script name instead. Example, click on a link to a document called 'Document.pdf'. The browser says it will download a PHP script called 'download.php'. 
  • When you save the file, it is actually the Document.pdf but saved as download.php.
  • Apache is executing the script and not downloading the script.

Technologies Used: 

  • Apache
  • PHP

Caused by:

  • This happens if the download is controlled by a PHP script instead of a direct link to the document. The PHP script sends out a header indicating the type of file that is coming through. The code to do this is:
header("Content-type: application/$ext");
header("Content-length: $fsize");
header("Content-Disposition: attachment; filename=$request[file]"); 


What I had was:
header("Content-type: application/$ext");
header("Content-length: $fsize");
header("Content-Disposition: attachment: filename=$request[file]"); 
 I typed a colon after the word 'attachment' instead of a semi-colon. Changing it to a semi-colon fixed the problem.
 
 
Copyright: © 2018 Philip Yuson