无法使用codeigniter下载文件

时间:2011-01-25 06:26:05

标签: php codeigniter

这是我的下载控制器。第一次它正常工作。它打开保存为弹出窗口,并能够下载所需的文件,但下次它显示直接目录列表。

enter image description here

<?php

class Download extends Controller {

 function Download(){
  parent::Controller();
  $this->load->helper('download');
  echo "I am in constructor";

 }

 function index(){
  $file = realpath("download")."\\profile.doc"; 
  echo "I am in index.";

        exit;

  if (file_exists($file)) {
      header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename=' . basename($file));
            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($file));
            ob_clean();
            flush();
            readfile($file);
            exit;
        }else{

   // File Not Found

         echo "File not found";
        }

 }
}
?>

1 个答案:

答案 0 :(得分:3)

您应该使用user_guide中显示的下载帮助程序。它涉及这种情况。