Php 如何使文件可下载

Php 如何使文件可下载,php,download,Php,Download,我正在尝试添加文件下载到我的网站音频视频和pdf文件 我正在使用php header()函数,但它没有下载输出文件,而是继续下载php页面。这是我的密码 header('Content-disposition: attachment; Content-type: audio/mpeg; filename=thefile.type'); 你需要把文件的内容写出来,例如 header('Content-disposition: attac

我正在尝试添加文件下载到我的网站音频视频和pdf文件 我正在使用php header()函数,但它没有下载输出文件,而是继续下载php页面。这是我的密码

    header('Content-disposition: attachment; 
            Content-type: audio/mpeg; 
            filename=thefile.type');

你需要把文件的内容写出来,例如

header('Content-disposition: attachment; 
   Content-type: audio/mpeg; 
   filename=thefile.type');
echo file_get_contents("thefile.type");
这很有效

    if (file_exists("audio/thefile.type"))
    {
         if (FALSE!== ($handler = fopen('thefile.type', 'r')))
            {
             header('Content-Description: File Transfer');
             header('Content-Type: application/octet-stream');
             header('Content-Disposition: attachment; 
                     filename=audio/thefile.type');
             header('Content-Transfer-Encoding: chunked'); //changed to chunked
             header('Expires: 0');
             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
             header('Pragma: public');
             //header('Content-Length: ' . filesize('thefile.type')); //Remove

             //Send the content in chunks
             while(false !== ($chunk = fread($handler,4096)))
                  {
                    echo $chunk;
                  }
             }
             exit;
    }
    else{

           echo "<h1>Content error</h1><p>The file does not exist!</p>";
         }
如果(文件_存在(“audio/thefile.type”))
{
if(FALSE!==($handler=fopen('thefile.type','r'))
{
标题(“内容描述:文件传输”);
标题(“内容类型:应用程序/八位字节流”);
标题('内容-处置:附件;
filename=audio/thefile.type');
标题('Content-Transfer-Encoding:chunked');//更改为chunked
标题('Expires:0');
标头('Cache-Control:必须重新验证,后检查=0,前检查=0');
标题(“Pragma:public”);
//标题('Content-Length:'.filesize('thefile.type');//删除
//分块发送内容
while(false!==($chunk=fread($handler,4096)))
{
echo$chunk;
}
}
出口
}
否则{
echo“内容错误文件不存在!

”; }
看这里:为什么不直接链接或重定向到该文件<代码>标题(“位置:thefile.type”)@谢谢,它现在可以工作了。
echo文件\u获取\u内容…
!哎呀。完全忘记了。谢谢你。