写入后force_download在codeigniter中不起作用

时间:2018-03-07 08:02:44

标签: codeigniter download

尝试在创建文件后自动下载文件,但它无法正常工作。文件创建和附加数据完美但下载功能不起作用



 public function getexportfile(){
       $this->load->dbutil();
     // $this->load->database('DB_utility','dbutil');
      //$this->load->helper('download_helper','download');
        $this->load->helper('download_helper');
        $this->load->helper('file');
      error_reporting(0);
      ini_set('display_errors', 0);
      $postdata = file_get_contents("php://input");
      $request = json_decode($postdata);
      
       ## Condition Start  
        $sWhere = " where condition"; // where conditon 
      
       $date = date('Y-m-d');
        $filename = 'app/logs/othermarketplace_'.$date.'.csv'; 
        
        $data = $this->PMP->export($sWhere); // calling model
        
        $result = $this->dbutil->csv_from_result($data);
        write_file($filename,$result);  // file creating write data perfectly
        
        $data = file_get_contents($filename);
        
        //$path    =   file_get_contents(base_url().$filename);
        //$name    =   "othermarketplace_'.$date.'.csv";
        //force_download($nme, $pth);  
        
        force_download($filename,$data);
        }




我错过了什么?

1 个答案:

答案 0 :(得分:0)

来自docs:

  

//将自动阅读photo.jpg的内容

     

force_download(' /path/to/photo.jpg',NULL);

    //$data = file_get_contents($filename);

    //$path    =   file_get_contents(base_url().$filename);
    //$name    =   "othermarketplace_'.$date.'.csv";
    //force_download($nme, $pth);  

    if (!is_file($filename)) {
        exit('No file found.');
    }

    force_download($filename, NULL);

注意:

这些线可能会做一些奇怪的事情。也许删除它们,因为它们没有被使用:

error_reporting(0);
ini_set('display_errors', 0);
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);