PHPページをクリックしてダウンロード

1647 ワード

 <a href="{:U('down')}">111</a>
public function down(){

            $id=I('get.id');
             $downLogic = D('Down','Logic');
             $where['id'] = $id;
             
             $down = $downLogic -> where($where) ->find();
             header("Content-type:text/html;charset=utf-8");
                 
             $url = $down['relativepath'];//      
             $filename = $down['downname'];//    
             //$filename=iconv("utf-8","gb2312",$filename);   //               
             
             //              
             if(!file_exists($url)){
                 echo "     ";
                 return;
             }
            $file = fopen($url,"r");        //    
            $filesize = filesize($url);
            
            //          
            Header("Content-type: application/octet-stream" ); //                          
            Header("Accept-Ranges: bytes" );    //                         
            Header("Accept-Length: " . $filesize );    //             
            Header("Content-Disposition: attachment; filename=".$filename );    //              
            
            $buffer = 1024;
            $file_count = 0;
            //        
            while(!feof($file) && $file_count<$filesize){
                $file_con = fread($file,$buffer);
                $file_count += $buffer;
                echo $file_con;
            }
            fclose($file);
    }