php下载文件

页面

$url = 'index.php?r=site/download&fn='.json_encode($filename.'.xls');
  echo "<iframe src='$url' style='display:none'></iframe>";

 

方法

public function Download()
 {
   $filedir = 'cache/';
   $fn = json_decode($_GET['fn']);
   $filename = iconv('utf-8','gbk',$fn);
   if (!file_exists($filedir.$filename)){
     header("Content-type: text/html; charset=utf-8");
     echo "File not found!";
     exit; 
  } else {
    $file = fopen($filedir.$filename,"r"); 
    header("Content-type: application/force-download");
    header("Accept-Ranges: bytes");
    header("Accept-Length: ".filesize($filedir.$filename));
    header("Content-Disposition: attachment; filename=".$filename);
    echo fread($file, filesize($filedir.$filename));
    fclose($file);
  }
 }


你可能感兴趣的:(php下载文件)