How to downlaod Files using CodeIgniter?

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);
    }

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

DMCA.com Protection Status