方法一:

    利用php中readfile函数读取文件内容到输出缓冲,然后再进行输出,效率比较低,文件必须通过PHP进行一次处理才能进行输出

<?php
 $file = 'test.zip';  
 header('content-type:application/octet-stream');  
 header('content-disposition:p_w_upload; filename='.basename($file));  
 header('content-length:'.filesize($file));  
 readfile($file);

方法二:

    这里和方法一是一样的,只不过采用file_get_contents函数来读取文件并输出

<?php
 $file = 'test.zip';  
 header('content-type:application/octet-stream');  
 header('content-disposition:p_w_upload; filename='.basename($file));  
 header('content-length:'.filesize($file));  
 echo file_get_contents($file);

方法三:

    基于Apache的mod_xsendfile扩展,来进行文件的输出,文件可以不经过php读取到内存,这在处理大文件[超过1G的文件]方面是一个比较可取的选择,首先可以检查一下apache是否安装改扩展:

rpm -q mod_xsendfile    --检查是否安装该扩展

yum install mod_xsendfile --执行该命令安装该扩展

service httpd restart    --安装扩展之后重启服务

httpd -M|grep xsendfile  --检查apache是否成功加载该扩展

    安装完该扩展之后,如需要在配置文件中添加该选项:

XSendFile On  --开启该选项后,需要重启服务

然后编辑一个php文件,测试是否能正常输出:

 header('content-disposition:p_w_upload; filename=test.zip');
 header("X-Sendfile:/var/www/test.zip");

注意文件的权限问题