File download function
1, add a hyperlink here on the front-end open button
The hyperlink carries the id of the file
2, Get the get request and execute the download configuration:
<?php //获取点击事件的get请求 $downloadfile_id=isset($_GET['download'])?intval($_GET['download']):""; //下载文件功能 if($downloadfile_id!=""){ $sql="select *from netdisk_file where file_id=$downloadfile_id"; if($downloadfile=fetchRow($sql)){ //获取文件大小 $file_size=filesize($downloadfile); //设置HTTP响应消息为文件下载 header('content-type :octet-stream'); header('content-length: '.$file_size); header('content-disposition: attachment;filename="'.$downloadfile['file_name'].'"'); //以只读的方式打开文件 $fp=fopen($downloadfile['file_save'],'r'); //读取文件并输出 $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);//关闭文件 //终止脚本 die; }else{ echo "文件不存在"; }; }
3, Page display: