0

我试过了

$file = file_get_contents("http://my_site/wp-content/uploads/2013/04/Dev-TipsTricks.pdf", true);
echo $file;

但浏览器显示特殊字符(见图)。

我也试过

$mime_type = getMimeType($extension[1]);
ob_start();
header("Content-type: $mime_type");
header("Content-disposition: attachment; filename=$file");
ob_clean();
flush();
readfile($upload_dir['baseurl'].'/'.$file);
exit;

当我从本地主机运行时它会下载文件,但在实时服务器上它也显示特殊字符,请参见图像。

我也跟着它file_get_contents

在此处输入图像描述

我也试过pdf

header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");

所有文件扩展名都会发生这种情况。

文件路径是正确的&如果我在新的浏览器选项卡中回显路径&复制/粘贴它会打开pdf &如果docx浏览器的路径允许下载。

浏览器以下警告显示:

Warning: Cannot modify header information - headers already sent by (output started at /home/me/public_html/my_site/wp-admin/includes/template.php:1638) in /home/me/public_html/my_site/wp-admin/download.php on line 224

这是template.php中的代码

function _wp_admin_html_begin() {
    $admin_html_class = ( is_admin_bar_showing() ) ? 'wp-toolbar' : '';
?>
<!DOCTYPE html>
<!--[if IE 8]>
<html xmlns="http://www.w3.org/1999/xhtml" class="ie8<?php echo $admin_html_class;?>"<?php do_action('admin_xml_ns');?><?php language_attributes();?>>
<![endif]-->
<!--[if !(IE 8) ]><!-->
<html xmlns="http://www.w3.org/1999/xhtml" class="<?php echo $admin_html_class;?>"<?php do_action('admin_xml_ns');?><?php language_attributes(); ?>>
<!--<![endif]-->
<head>
<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_option('blog_charset'); ?>" />
<?php
}

现在我无法弄清楚错误在哪里......?

在此处输入图像描述

4

2 回答 2

1
header("Content-type: 'application/pdf'");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=filename.pdf"); 
    echo $content;

在这里你必须设置“内容”。这段代码对我有用。当我从数据库中获取这个“内容”时,它在我的数据库中保存为 .pdf 文档。

于 2013-05-08T12:07:12.620 回答
0

尝试将其添加到 .htaccess

AddType application/pdf pdf
于 2013-05-08T12:08:43.807 回答