downloading PDFs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

downloading PDFs

Post 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
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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;
Post Reply