I had some problems with a php download script. It's a pdf file and after hitting download, it throws the whole file as plain text instead of downloading it. Happens for jpeg and other files too.
This error only happens, once I wrapped a class around the code ->
<?php
class BaseClass {
function __construct() {
$this->makeDownload();
}
public function makeDownload()
{
$downloadfile = "./downloads/file.pdf";
$filename = "file.pdf";
$filesize = filesize($downloadfile);
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename='$filename'");
header("Content-Length: $filesize");
readfile($downloadfile);
}
}
// In BaseClass constructor
$obj = new BaseClass();
?>
The previouse webspace runs this code without any problems. The new webspace, as I wrote, throws something like this ->
%PDF-1.5 %âãÏÓ 144 0 obj <> endobj xref 144 41 0000000016 00000 n 00000.....
Without the wrapped class, everything works(downloads the pdf). So it's no the method what causes the error.
Is there any setting in the php.ini on the other webspace, which could cause a download wrapped in a class spams with the files sourcecode instead of downloading it?