Page 1 of 1

downloading PDFs

Posted: Fri Oct 13, 2006 4:25 am
by hame22
Hi

I have set up a site where the user can download a pdf .

The code I am using is as folllows:

Code: Select all

$TJ_PDF_PATH = '/usr/local/home/httpd/vhtdocs/host/web/assets/news/ ';

$path 		=''.$TJ_PDF_PATH.''.$news_pdf.'';
	
	
	if (file_exists($path)) 
	{
		$size 	= filesize($path);
		$file 	= basename($path);
		
		header("Content-Type: application/octet-stream");
		header("Content-Type: application/download");
		header("Content-Disposition: attachment; filename=$file");
		header("Content-Length: $size");
		readfile($path);
		header("Location: $path");
	
}
else {
	print 'There was a problem with the download, please contact for assistance';
}
This works fine when I want to save the pdf to my system, howeve when I choose to open it there is an error saying the file could not be found. - This error is only expeirenced in IE

Does anyone know where I have gone wrong here?

Thanks in advance

Posted: Fri Oct 13, 2006 6:16 am
by akimm
This is just a shot in the dark for the record, but check the function dirname() and some corresponding functions, perhaps basename() isn't what IE needs for this to work; what happens in IE when this error occurs, does it take you to a wrong page, no output, or what?

For an example of dirname()
This is untested, and probably will fail, but hopefully I helped, otherwise, I may motivate someone better to help you at least :D

Code: Select all

<?php
$path = "/downloads/";
$directory = dirname($path);
if(is_dir($path)){
 $file = "/downloadme.pdf";
} else {
echo "directory doesn't exist contact admin";
}
if(file_exists($file)) {
$size   = filesize($path);
                $downloadfile   = basename($file);

                header("Content-Type: application/octet-stream");
                header("Content-Type: application/download");
                header("Content-Disposition: attachment; filename=$downloadfile");
                header("Content-Length: $size");
                readfile($path);
                header("Location: $path");

}
else {
        echo 'There was a problem with the download, please contact for assistance';
}
?>
I

Posted: Fri Oct 13, 2006 6:48 am
by Chris Corbyn
Running over HTTPS by any chance?

EDIT | By the way, why not change:

Code: Select all

$path     =''.$TJ_PDF_PATH.''.$news_pdf.''; 

//to this
$path     = $TJ_PDF_PATH.$news_pdf;