php隐藏文件下载路径实例

如果我们需要隐藏下载文件路径我们只要直接输入就可以了,而不需要跳转路径,下面我们来看一个实例,希望对各位同学会有所帮助,代码如下:

  1. <?php
  2. //设置头信息,强制下载文件
  3. function download_send_headers($filename) {
  4. // disable caching
  5. $now = gmdate("D, d M Y H:i:s");
  6. header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
  7. header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
  8. header("Last-Modified: {$now} GMT");
  9. // force download
  10. header("Content-Type: application/force-download");
  11. header("Content-Type: application/octet-stream");
  12. header("Content-Type: application/download");
  13. // disposition / encoding on response body
  14. header("Content-Disposition: attachment;filename={$filename}");
  15. header("Content-Transfer-Encoding: binary");
  16. }
  17. $file_name='download.csv';
  18. $file_path=dirname ( __FILE__ ).'/file/'.$file_name;
  19. download_send_headers($file_name);
  20. readfile($file_path);
  21. exit;
  22. ?>