Another problem with PHP force download. It was working and then i did something and now its not.
Here we go (there are if statements for each file type I accept but just showing one should be enough to help diagnose the problem):
$string1 = 'home/myhost/aboveroot/reviews/';
$string2 = $username;
$string3 = '/';
$string4 = $filename;
$file= $string1.$string2.$string3.$string4;
$ext = strtolower (end(explode('.', $filename)));
//Finding MIME type
if ($ext = pdf){
header("Content-disposition: attachment; filename= '$filename'");
header('Content-type: application/pdf');
readfile('$file');
What ends up happening is that it forces a file download with the correct file name and of the correct extension. It doesn’t trigger a corrupt file error of any sort. However, when I open the file the content is missing. If its a word document it will refuse to open. If its a text document the text will be changed to a bunch of php errors as follows:
Warning: readfile($file) [function.readfile]: failed to open stream: No such file or directory in /home/myhost/public_html/wwww.mydomain.com/pagethatforceddownload.php on line 68
Then it gives this error for each line with a header (even those contained in if clauses that should not be meeting their conditions:
Warning: Cannot modify header information – headers already sent by (output started at /home/myhost/public_html/wwww.mydomain.com/pagethatforceddownload.php:68) in /home/myhost/public_html/wwww.mydomain.com/pagethatforceddownload.php on line 69
I’m really lost. Any help would be greatly appreciated. I finally get everything on my site working and BAM, I realize that im having this huge mess of a problem.
This line is wrong :
It should be :
And you could add :
to be sure that you get a valid file.
And this is wrong too :
It should be :