Vicky
Vicky

Reputation: 9585

Unable to download file using codeigniter

This is my down loader controller. First time it works properly. it opens save as popup and able to download required file, But next time it shows direct directory listing.

enter image description here

<?php

class Download extends Controller {

 function Download(){
  parent::Controller();
  $this->load->helper('download');
  echo "I am in constructor";

 }

 function index(){
  $file = realpath("download")."\\profile.doc"; 
  echo "I am in index.";

        exit;

  if (file_exists($file)) {
      header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename=' . basename($file));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            ob_clean();
            flush();
            readfile($file);
            exit;
        }else{

   // File Not Found

         echo "File not found";
        }

 }
}
?>

Upvotes: 0

Views: 2569

Answers (1)

Teej
Teej

Reputation: 12873

You should use the download helper as seen in the user_guide. It deals with this type of situation.

Upvotes: 3

Related Questions