2012-03-29 61 views
0

下载我需要通过头下载文件帮助其工作正常,但问题是,该文件是一个文件夹中的地方,我不知道如何给路径特定文件。文件下载总是给我空文件。我的基本sanario是,我点击一个链接(图片)下载该送我到一个名为download.php.in该网页我设置标头下载点击文件好像文件通过头

其他页面命名网页上的文件(documents.php)

这是一个链接(document.php)

<a href="download.php?title=<?=$arr['title']?> "><img src="images/xl_icn.jpg" alt="" width="19" height="20" /></a> 

这是头(的download.php)

header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); 

header("Content-Disposition: attachment; filename='".$_GET['title']."'"); 

if(isset($_SESSION)){ 
    header('Location: documents.php'); 
} 

和下载它送我回documents.php网页,但这些文件之后,我想下载是在文件夹上传/文件,我不知道如何下载该文件页在上传/文件中。

+0

我会建议你使用这个脚本http://www.zubrag.com/scripts/download.php – Kamal 2012-03-29 07:13:18

回答

0

你有一个良好的开端我会修改你的代码更类似如下:

$path = $_SERVER['DOCUMENT_ROOT']."/uploads/documents/"; 
$fullPath = $path.$_GET['title']; 

if ($fd = fopen ($fullPath, "r")) { 
    $fsize = filesize($fullPath); 
    header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); 
    header("Content-Disposition: attachment; filename='".$_GET['title']."'"); 
    header("Content-length: $fsize"); 
    header("Cache-control: private"); 
    while(!feof($fd)) { 
    $buffer = fread($fd, 2048); 
    echo $buffer; 
    } 
} 
fclose ($fd); 
您将更新

$路径是什么文件夹 if语句工作所需的文件调用和回声在缓冲区风格的页面内容。

这可以进一步扩大到包括根据扩展来改变内容类型声明的switch语句。由于您将内容缓存到页面,因此不能拨打header("Location");。或者由于缓冲,你会得到“因为页面内容已经加载而无法发送标题”。

我建议在新窗口或页面上的IFRAME(包裹在一个div,并可能被隐藏)打开它。您也可以将其包含在document.php文件中,而不是调用单独的文件。

+0

这么好的ü亲爱的兄弟 – user1210460 2012-03-29 10:22:43

0

您不能从下载页面重定向回。应的download.php只是做

echo $binary_file_contents;

和退出,无法发送重定向报头。

您可以在iframe中打开它,这样你就不会真正离开documents.php页面

相关问题