Posted by Arjun on Wednesday 16th October 2013

How to downlaod Files using CodeIgniter?

CodeIgniter CodeIgniter Download Helper CodeIgniter Helper

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
            // get download file path and store it in $data array
            $data['download_file'] = $file_path;    
            // load view file    
            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);