PHP编程之旅----http、文件下载

<?php
//	关于http协议的各种解析  百度/google
//	防止盗链
if(isset($_SERVER["HTTP_REFERER"])){  
	//取出来,判断是不是https://192.168.1.110/myphp/http开头
	if(strpos($_SERVER["HTTP_REFERER"],"https://192.168.1.110/myphp/http") == 0){  
		echo "something";  
	}  
} else{ //页面跳转
	header("Location: http://www.baidu.com/");  
}  

//页面无反应,定时跳转页面
header("Refresh:3;url=http://www.baudu.com");

//通过header来禁用缓存
header("Expires:-1");
header("Cache-Control:no_cache");
header("Pragma:no-cache");
?>


<?php
	//	download file
	$file = "source.pdf";
	if(!file_exists($file)){
		echo 'file not found';
		exit();
	}
	//	对于存在显示乱码的问题,需要在打开文件前对,文件名进行转码
	$file=iconv("utf-8","gb2312",$file);  
	// open the file and get size of the file
	$fp = fopen($file,"r");
	$file_size = filesize($file);
	//	开始下载
	header("Content-type:application/octes-stream");
 	//	按照字节大小返回  
    header("Accept-Ranges: bytes");  
    //	返回文件的大小  
    header("Accept-Length: $file_size");  
    //	这里是客户端弹出的对话框,对应的文件名  
    header("Content-Disposition: attachment;filename=".$file); 
    //	向客户端返回数据  
    $buffer=1024;  
    //	为了下载的安全,我们最好做一个文件字节读取的计数器  
    $file_count=0;  
    //	判断文件是否下载结束  
    while(!feof($fp)&&($file_size-$file_count >0)){  
        $file_data=fread($fp,$buffer);  
        //统计读了多少字节  
        $file_count += $buffer;  
        //把部分数据回送给浏览器;  
        echo $file_data;  
    }  
    //关闭文件  
    fclose($fp);  
?>


发布了40 篇原创文章 · 获赞 15 · 访问量 3万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章