Download File in php or codeigniter

Want to download file using php. Here is the sample code. By using it you can download the file in php. you can use it in Codeigniter Also. the function are tested in Codeigniter. So it will works file in Codeigniter.
 Just Call the function,
It will start Downloading the file.
 public function download_sample(){

        $filepath = '../dir/sample/';

        $filename = 'file1.txt';

        $this->download_now($filepath, $filename);

public function download_now($filepath='', $filename='' ){

        header('Content-Description: File Transfer');

        header('Content-Type: application/force-download');

        header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\";");

        header('Content-Transfer-Encoding: binary');

        header('Expires: 0');

        header('Cache-Control: must-revalidate');

        header('Pragma: public');

        header('Content-Length: ' . filesize($filename));




        readfile($filepath . $filename); 



Leave a Reply

Your email address will not be published. Required fields are marked *