PHP:强制下载PDF文件

PHP:强制下载PDF文件,php,html,Php,Html,我正在尝试下载一个.pdf文件,该文件位于“提交”按钮上,下面是我的代码 if(mail('myemail@gmail.com', 'Brochure Downloaded ', $string)){ $text= 'Your message recieved, We will contact you shrtly '; $file_url = 'http://www.website.com/brochure.pdf';

我正在尝试下载一个.pdf文件,该文件位于“提交”按钮上,下面是我的代码

if(mail('myemail@gmail.com', 'Brochure Downloaded ', $string)){ 
            $text= 'Your message recieved, We will contact you shrtly '; 
            $file_url = 'http://www.website.com/brochure.pdf'; 
                header('Content-Type: application/octet-stream');
                header("Content-Transfer-Encoding: Binary"); 
                header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); 
                readfile($file_url); // do the double-download-dance (dirty but worky)
    }
现在发生的事情是,电子邮件被发送,但文件没有被下载,而是页面被重新加载,长字符被打印在屏幕上

需要你帮忙吗


这就是我得到的

我对标题做了一些更改。我认为合适的内容类型是
application/pdf
,一些标题是不必要的

if(mail('myemail@gmail.com', 'Brochure Downloaded ', $string)){ 
  $text= 'Your message recieved, We will contact you shrtly '; 
  $file_url = 'http://www.website.com/brochure.pdf'; 
  header("Content-type:application/pdf");
  header("Content-Disposition:attachment;filename='" . basename($file_url) . "'");
  readfile($file_url); // do the double-download-dance (dirty but worky)
}
您还可以添加内容长度:

header('Content-Length: ' . filesize($file_url));

我改变了一些标题。我认为合适的内容类型是
application/pdf
,一些标题是不必要的

if(mail('myemail@gmail.com', 'Brochure Downloaded ', $string)){ 
  $text= 'Your message recieved, We will contact you shrtly '; 
  $file_url = 'http://www.website.com/brochure.pdf'; 
  header("Content-type:application/pdf");
  header("Content-Disposition:attachment;filename='" . basename($file_url) . "'");
  readfile($file_url); // do the double-download-dance (dirty but worky)
}
您还可以添加内容长度:

header('Content-Length: ' . filesize($file_url));

检查此代码,如果您下载任何类型的文件,首先需要添加内容类型,就像您要下载图像一样 标题('Content-Type:image/png');如果是pdf格式,那么 标题(“内容类型:application/pdf”);等


如果您希望有人下载带有另一个标题的文件,则应使用内容长度检查此代码,如果您下载任何类型的文件,则首先需要添加内容类型,就像您要下载图像一样 标题('Content-Type:image/png');如果是pdf格式,那么 标题(“内容类型:application/pdf”);等


如果您希望有人下载具有另一个标题的文件,您应该使用Content Length(内容长度)

您不需要此文件的内容长度吗?我只需要在浏览器的检查器中检查标题是否设置正确。您的服务器可能正在覆盖它们。我应该在哪里在inspector中进行检查?您使用的是哪种浏览器?您不需要为此设置内容长度吗?我只需要在此检查您的浏览器的inspector,以查看标题是否设置正确。您的服务器可能正在覆盖它们。我应该在Inspector中将其签入何处?您正在使用哪个浏览器?