Jump to content

Content-Disposition: attachment; filename=... not working as i thought it should


Recommended Posts

taken the following code from the php.net site the script is not working.

 

what is not happening is the file does not show and also the file download does not show the correct file name it seems to have the file name but has replaced the  /  with an underscore.

 

 

<?
$PDFtoview = "minutes/PDF filename.pdf";


// outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="' . $PDFtoview . '"');

// The PDF source is in original.pdf
readfile("'" . $PDFtoview . "'");
//echo($siteurl.$filename);
}
?>

The value for the filename should be just that, a file name (not a path). It should be just "PDF filename.pdf"

 

The readfile function expects its parameter to be the path to a file, yet you wrap that path in single quotes. The result is not a valid path so no file can be read.

 

P.S. You state that the code was from php.net, could you link me to the exact page where you got the code from?

ok the file now shows correctly in the browser.

 

but the download link does not work.

 

i use the index.php?file=123 method and the script would put the correct file in the header to be downloaded.

 

i am still unsure what line (header) does what...

 

<?
$uploaddir = "/home/site/public_html/";

$PDFdoc = "PDF filename.pdf";
$PDFtoview = "folder/" . $PDFdoc;

// outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="' . $PDFdoc . '"');

// The PDF source is in original.pdf
readfile("'" . $uploaddir . $PDFtoview . "'");
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb" >
<head>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.