Wednesday, October 4, 2017

Without login user can't download the document through browser or any way

Just you can use which is suitable below two lines in the function
                              header("Content-type:application/pdf");
                              header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
This .htaccess should be placed in documents path.

Note :  Copy and paste below two lines of codes only in .htaccess file, this .htaccess file should be placed in documents path.

order deny, allow
deny from all


Example url :  
https://localhost/projectname/File_downloads/doc_download/MTEvZG9jdW1lbnRzL2RvYzEucGRm

<?php
class File_downloads extends CI_Controller {
        public function __construct() {   
        date_default_timezone_set('Asia/Kolkata');
        parent::__construct();
        $this->load->helper('download');
    }
    
    
    public function index(){
       redirect('login');
    }
    
    public function download($file_name=NULL){
    if($this->session->userdata('logged_in')) { // login check 
           $file_name= base64_decode($file_name); 
           $name=end(explode('/',$file_name));
           if($file_name)
               {        
                        $file_url= realpath ( "uploads" ) . "/" . $file_name; 
                        if (file_exists($file_url)) // check the file is existing 
                            {
                               header("Content-type:application/pdf");
                               // It will be called downloaded.pdf
                                header("Content-Disposition:attachment;filename='downloaded.pdf'");  
                               $file_data= file_get_contents($file_url);
                               force_download($name,$file_data);

                            }
                        else 
                            {
                               show_404();
                            }  
                }
             else
                {
                  show_404();
                }   
                           
        }
       else 
       {
           redirect('login');
       }    
}



public function doc_download($file_name=NULL){
    if($this->session->userdata('logged_in')) { // login check 
           $file_name= base64_decode($file_name); 
           $name=end(explode('/',$file_name));
           if($file_name)
               {        
                        $file_url= realpath ( "uploads" ) . "/" . $file_name; 
                        if (file_exists($file_url)) // check the file is existing 
                            {
                              // header("Content-type:application/pdf");
                               // It will be called downloaded.pdf
                                //header("Content-Disposition:attachment;filename='downloaded.pdf'");  
                               $file_data= file_get_contents($file_url);
                               force_download($name,$file_data);

                            }
                        else 
                            {
                               show_404();
                            }  
                }
             else
                {
                  show_404();
                }   
                           
        }
       else 
       {
           redirect('login');
       }    
}
   
}
?>

No comments:

Post a Comment