您的位置:首页 > 编程语言 > PHP开发

php实现文件强制下载

2013-06-02 00:00 253 查看
为什么要实现php文件强制下载呢?一般文件现在用a链接就可以了,现在的浏览器都很牛x啊,什么txt,pdf,excel等待都能实现,直接在浏览器上打开了,如果你不想这么做,那么用php去实现。其实我在之前说php header函数的时候就已经说过了。简单实现原理如下:

<?php
	$file_dir = "./";	
	$name = "test.txt";
	$file = fopen($file_dir.$name,"r"); 
	Header("Content-type: application/octet-stream");
	Header("Accept-Ranges: bytes");
	Header("Accept-Length: ".filesize($file_dir . $name));
	Header("Content-Disposition: attachment; filename=".$name);
	echo fread($file, filesize($file_dir.$name));
	fclose($file);
?>

读者只要去改良下就行了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: