php标题问题

时间:2010-10-12 10:41:38

标签: php

我正在尝试使用以下代码屏蔽我的下载链接。

几乎正在工作 - 它似乎正确地获取文件但是当它下载文件时只有4kB大小。

任何人都可以提供任何建议吗?

谢谢!

如果您需要任何其他详细信息,请告知我们 - 我在MAMP上使用FF3.5.13进行此操作

<?php

    $filename="download.zip";

    $folder = 'downloads'; 

    $abs_path = $_SERVER['DOCUMENT_ROOT'];
    $path = $abs_path . "/" . $folder ."/" .$filename; // the location of the file.
    $mm_type="application/zip"; //this is for .zip files - Change this for other file types.

    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Type: " . $mm_type);
    header('Content-Disposition: attachment; filename="'.basename($path).'"');
    header("Content-Length: " . filesize($path)); // **code edited as per comments below**
    header("Content-Transfer-Encoding: binary");

    readfile($path); //Output file for download.

    exit();

?>

更新:这是生成的文件内部的内容


警告:filesize()[function.filesize]: /Applications/MAMP/htdocs/CURRENT/test.php中/Applications/MAMP/htdocs/downloads/download.zip的统计信息失败< / b>在线 15

警告:无法修改标头信息 - 已在 / Applications / MAMP / htdocs / CURRENT /中发送的标头(在/Applications/MAMP/htdocs/CURRENT/test.php:15处开始输出) test.php 15
警告:无法修改标头信息 - 已在 / Applications / MAMP / htdocs / CURRENT /中发送的标头(在/Applications/MAMP/htdocs/CURRENT/test.php:15处开始输出) test.php 16 行上
警告:readfile(/Applications/MAMP/htdocs/downloads/download.zip)[function.readfile]:无法打开流: / Applications / MAMP / htdocs中没有此类文件或目录/CURRENT/test.php 17

4 个答案:

答案 0 :(得分:3)

我认为这是你错的地方:

header("Content-Length: " . filesize($file));

将其更改为:

header("Content-Length: " . filesize($path));

答案 1 :(得分:1)

header("Content-Length: " . filesize($file)); 

$ file被设置在哪里?

答案 2 :(得分:1)

此消息

  

警告:filesize()[function.filesize]:第15行的/Applications/MAMP/htdocs/CURRENT/test.php中/Applications/MAMP/htdocs/downloads/download.zip的统计信息失败

足够清楚:您的文件不存在。

答案 3 :(得分:0)

尝试:

header ("Content-Disposition: attachment; filename=".$name."\n\n"); 

header ("Content-Type: application/octet-stream"); 

header ("Content-Length: ".filesize($complete_path_file)); 

readfile($complete_path_file); 

或将内容类型更改为标题(“内容类型:应用程序/强制下载”);