简体   繁体   English

PDF文件未下载

[英]PDF File is not downloading

I have the following file.php . 我有以下file.php My PDF is in a files folder, file.php , and its file are in the same directory. 我的PDF位于文件文件夹file.php ,其文件位于同一目录中。 When I press the button, the PDF is not downloading but the file.php is. 当我按下按钮时,PDF未下载,但file.php已下载。

<?php
  if (isset($_POST['file_name'])){
    $file= $_POST['file_name'];
    header('Content- type: application/pdf');
    header('Content-Disposition: attachament; filename: " .$files"');
    readfile('files/'.$file);
    exit(); 
  }
?>
<form action="file.php" method="POST" >
  <input name="file_name" value="cv.pdf" type="hidden">
  <input type="submit" value="Download CV">
</form>

No need of all that. 不需要所有这些。 Just try this: 尝试一下:

<a href="/files/yourfullPathOnTheServer.format" download>

You can replace a href with button :) 您可以将href替换为按钮:)

try with this if you want to download pdf using php code,set file path to $file 如果您想使用php代码下载pdf,请尝试将其设置为$file

<?php
 $file = 'filename.pdf';
 if(!file){
     die('Error: file not found');
 }else{
     header("Cache-Control: public");
     header("Content-Description: File Transfer");
     header("Content-Disposition: attachment; filename=$file");
     header("Content-Type: application/pdf");
     header("Content-Transfer-Encoding: binary");
     readfile($file);
 }
 ?>

You need to just change the below code 您只需要更改以下代码

header('Content-Disposition: attachament; filename: " .$files"');

with this: 有了这个:

header("Content-Disposition: attachament; filename: '$files'");

Also remember that 还记得

It is important to notice that header() must be called before any actual output is sent (In PHP 4 and later, you can use output buffering to solve this problem) 重要的是要注意必须在发送任何实际输出之前调用header()(在PHP 4和更高版本中,可以使用输出缓冲来解决此问题)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM