【问题标题】:Download PHP/HTML/CSS to PDF Document下载 PHP/HTML/CSS 到 PDF 文档
【发布时间】:2014-10-30 14:01:13
【问题描述】:

这里我有一些代码,它按照它说的去做并下载,但是只下载一个白色空白的空文件,我正在尝试使用这个 Wordpress 并且必须没有插件。该脚本有效,但它没有将任何信息下载到 PDF 文档中,它只是一个白色的空白页,这里是代码,正在寻找将 Wordpress 帖子添加到文档的解决方案,这里是链接和代码,理想情况下我想要它下载执行链接的页面。

HTML 链接

<a href="http://www.WEBSITE.com/wp-content/themes/the-theme/download.php?download_file=some_file.pdf">Download file</a>

下载.php

<?php

ignore_user_abort(true);

$path = ""; // change the path to fit your websites document structure
$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\].]|[\.]{2,})", '', $_GET['download_file']); // simple file name validation
$fullPath = $path.$dl_file;

if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
    case "pdf":
    header("Content-type: application/pdf");
    header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download
    break;
    // add more headers for other content types here
    default;
    header("Content-type: application/octet-stream");
    header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    break;
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
    $buffer = fread($fd, 2048);
    echo $buffer;
}
}
fclose ($fd);
exit;
?>

【问题讨论】:

    标签: php wordpress pdf


    【解决方案1】:

    请勿使用以上代码!

    它不仅不会将任何内容转换为 PDF,而且还允许全世界下载网络服务器可读的纯文本格式的任意文件。因此,如果在 wp-config.php 中设置,则可以下载您的 mysql 数据库凭据或 FTP 密码。

    解释:代码没有转换任何东西。它只是获取任意文件的内容并以 PDF 或八位字节流内容类型输出。使用文本编辑器查看您下载的“PDF”。


    看来你还没有设置线 $path = ""; // change the path to fit your websites document structure 指向指向some_file.pdf 的完整路径。

    【讨论】:

    • 它下载文件,但它是一个空白的pdf,我也查看了路径,但它只在空白时下载,如果它有一个结构它会崩溃并给出多个错误
    猜你喜欢
    • 1970-01-01
    • 2017-12-18
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 2013-03-22
    相关资源
    最近更新 更多