qqwe 发表于 2015-8-26 11:05:03

PHP下载文件的两种方法与代码分享

  PHP实现下载文件的两种方法。分享下,有用到的朋友看看哦。
  方法一:



<?php
/**
* 下载文件
* header函数
*
*/
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($filepath));
header('Content-Transfer-Encoding: binary');
header('Expires: 0&prime;);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0&prime;);
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
readfile($file_path);
?>
  以上代码用到了php header函数,可以参考以下如下的文章:
php header()函数的简单例子
php
header函数实现文件下载的实例代码
php中header函数的用法举例详解
php header
使用详解
php header函数
文件下载时直接提示保存的代码
php
header函数实现文本文件下载的方法
php
文件头部(header)信息详解
php使用header发送各种类型文件下载的例子
了解php中header函数的用法。
  方法二:



<?php
//文件下载
//readfile
$fileinfo = pathinfo($filename);
header('Content-type: application/x-'.$fileinfo['extension']);
header('Content-Disposition: attachment; filename='.$fileinfo['basename']);
header('Content-Length: '.filesize($filename));
readfile($thefile);
exit();
?>
页: [1]
查看完整版本: PHP下载文件的两种方法与代码分享