【发布时间】:2014-04-05 20:26:21
【问题描述】:
下面的 .php 脚本对我不起作用 - 它只会打印乱码:
<?php
$path = $_SERVER['DOCUMENT_ROOT']."/rapcDemo/documents/";
$fullPath = $path.$_GET['download_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"); // add here more headers for diff. extensions
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
break;
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
}
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);
?>
任何想法我做错了什么?
【问题讨论】:
-
您可能没有发送正确的内容类型。
-
它正在打印返回的内容。当您说“胡言乱语”时,您是什么意思?例如,如果您尝试回显图像,您会得到看起来很乱的文字
-
我正在尝试下载 .pdf 文档。它返回:
-
%PDF-1.4 %���� 22 0 obj endobj 44 0 obj /Filter/FlateDecode/ID[]/Index/22 36Info] 21 0 R/Length 101/Prev 69012/Root 23 0 R/Size 58/Type/XRef/W[1 2 1]>>流 h�bbd
b`�$�� ��{@bA� V!�u D<�A�ˀ�d �?�DH0n@BzL��HL�e`bdX �����?�ݯy�A endstream endobj startxref 0 %%EOF 57 0 obj <>stream h�b```g2aa����π����@q �����488@�]��������W����� ȓ��H ���3 endstream endobj -
我注意到当我输入“exit;”时在'fclose($fd);'之后声明,页面根本不会显示。是“出口”吗?命令真的很有必要(我是 php 新手)