Php 下载的文件保存为0kb并已损坏

Php 下载的文件保存为0kb并已损坏,php,file,download,Php,File,Download,我的下载脚本有问题 当我下载任何文件时,它是用0kb保存的。 有什么想法吗?这是我的PHP脚本 <?php $file = $_GET['fname']; $fullpath = get_bloginfo('template_url')."/application/display/1358947404.pdf"; header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment

我的下载脚本有问题 当我下载任何文件时,它是用0kb保存的。 有什么想法吗?这是我的PHP脚本

<?php
$file = $_GET['fname'];
$fullpath = get_bloginfo('template_url')."/application/display/1358947404.pdf";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fullpath));
header('Expires: 0');
header('Cache-Control: must-revalidate');
ob_clean();
flush();
readfile($fullpath);
?>

请同时添加内容长度标题

请试试这个:-

<?php
$file = $_GET['fname'];

$fullpath = get_bloginfo('template_url')."/application/display/1358947404.pdf";

$headers = get_headers($fullpath, 1);// use file absolute path here
$fsize = $headers['Content-Length'];

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fullpath));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header("Content-Length: " . $fsize);
ob_clean();
flush();
readfile($fullpath);
?>

尝试下面的代码,希望它能帮助您

我已经在本地主机上测试过了

$file = $_GET['fname'];

$fullpath = get_bloginfo('template_url')."/application/display/1358947404.pdf";
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fullpath));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($fullpath));
ob_clean();
flush();
readfile($fullpath);

标题(“内容长度:”.filesize($file));在我的例子中,$headers['Content-Length']显示空白,请告诉我问题出在哪里?请检查
get_headers()
函数中的绝对路径<代码>$fullpath=”http://yourdomain.com/directory/to/display/1358947404.pdf";打印(获取标题($fullpath,1))看起来像数组([0]=>[1]=>//www.w3.org/TR/xhtml1/DTD/xhtml1 strict.DTD“>[[2]=>[3]=>[[4]=>[6]=>)嘿,Niraj请参考get_headers()的工作原理。