【发布时间】: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;
?>
【问题讨论】: