How to download the xls file using codeigniter?

by elise_daugherty , in category: PHP Frameworks , a month ago

How to download the xls file using codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lottie , a month ago

@elise_daugherty 

To download an xls file in Codeigniter, you can use the following steps:

  1. Create a controller method that handles the download of the xls file. For example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public function download_xls()
{
    // Load the download helper
    $this->load->helper('download');
    
    // Set the path to the xls file
    $file_path = './path/to/your/file.xls';
    
    // Check if the file exists
    if (file_exists($file_path))
    {
        // Read the file
        $data = file_get_contents($file_path);
        
        // Set the file MIME type
        $mime = mime_content_type($file_path);
        
        // Set the file name
        $file_name = basename($file_path);
        
        // Force the browser to download the xls file
        force_download($file_name, $data, $mime);
    }
    else
    {
        // File not found error message
        echo 'File not found!';
    }
}


  1. Create a link in your view file that points to this controller method. For example:
1
<a href="<?php echo base_url('controller_name/download_xls'); ?>">Download XLS</a>


Replace 'controller_name' with the actual name of your controller.

  1. Make sure to replace the file path in the controller method with the actual path to your xls file.
  2. When the user clicks on the download link, the xls file will be downloaded to their browser.


That's it! Your users should now be able to download the xls file using the Codeigniter framework.