Skip to content

How to downlaod Files using CodeIgniter?

Last updated on January 17, 2018

CodeIgniter has a nice helper function to download files. It is a nice way to download from server without any hassle just writing one single controller method.

      public function download ($file_path = "") {
            // load ci download helder
            $this->load->helper('download'); 
            // get download file path and store it in $data array
            $data['download_file'] = $file_path;    
            // load view file    
            $this->load->view("download_view",$data);
            redirect(current_url(), "refresh");                       
        }

In view section(download_view.php), just call the download function thats it.

    if( ! empty($download_file))
    {
        //If you want to download an existing file from your server you'll need to read the file into a string
        $data = file_get_contents(base_url("/path/".$download_file)); // Read the file's contents
        $name = $download_file;
        force_download($name, $data);
    }
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments