如何在Codeigniter中下载Excel和doc文件?

时间:2019-01-12 11:54:54

标签: php codeigniter

我正在使用此代码,但无法下载exceldoc文件。

public function download_document($fileName=null){
    if ($fileName) {
        $file = realpath ( "uploads/user_img" ) . "\\" . $fileName;
        // check file exists    
        if (file_exists ( $file )) {
            // get file content
            $data = file_get_contents ( $file );
            //force download
            force_download ( $fileName, $data );
        } else {
            // Redirect to base url
            redirect ( base_url () );
        }
    }
}

1 个答案:

答案 0 :(得分:0)

请使用以下代码:-

public function download_document($fileName=null){
    $this->load->helper('download');
    if ($fileName) {
        $file = "uploads/user_img/" . $fileName;
        // check file exists    
        if (file_exists ( $file )) {

            //force download
            force_download ( $file, NULL );
        } else {
            // Redirect to base url
            redirect ( base_url () );
        }
    }
}

请点击以下链接。

https://www.codeigniter.com/userguide3/helpers/download_helper.html