PHP隐藏下载路径

这篇文章主要介绍了php隐藏实际地址的文件下载方法,涉及php中header与file_get_contents方法的相关使用技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了php隐藏实际地址的文件下载方法。分享给大家供大家参考。具体如下:


下面这段php代码可不透露实际的文件下载地址。

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′);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0′);
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
readfile($file_path);

代码确实能下载文件,但是,当文件过大的时候,就会出现意想不到的错误:

下载的文件莫名变小

下载的文件网络错误



以下为大文件正常下载代码:

<?php
	$down_file = "1.txt";//文件的真实地址(支持url,不过不建议用url)
	$file_info = pathinfo($down_file); //获取文件的名称,后缀等信息
	$filesize=filesize($down_file)+1000;
	header('Content-Description:File Transfer');
	header("Content-Type:application/octet-stream");
	header('Content-Transfer-Encoding:binary');
	header("Accept-Ranges: bytes");
	header('Expires:0');
	header('Cache-Control:must-revalidate');
	header('Pragma:public');
	header("Content-Length:".$filesize);
	header("Content-Disposition:attachment;filename=".$file_info['basename']);
	$fp = fopen($down_file, "rb");
	fseek($fp,0);
	while (!feof($fp)) {
		set_time_limit(0);
		print (fread($fp, 1024 * 8));
		flush();
		ob_flush();
	}
	fclose($fp);
	exit ();
?>


白俊遥博客
请先登录后发表评论
  • 最新评论
  • 总共0条评论