Geetika
Geetika

Reputation: 820

How to download a text file on link click in codeigniter

I have text file contains Sample of CSV file format, I want my users can download that file on a link click.

This file resides in this folder stucture:

assets->csv->Sample-CSV-Format.txt

This is the code that I have tried to far:

<?php
   $file_name = "Sample-CSV-Format.txt";

   // extracting the extension:
   $ext = substr($file_name, strpos($file_name,'.') + 1);

   header('Content-disposition: attachment; filename=' . $file_name);

   if (strtolower($ext) == "txt") {
       // works for txt only
       header('Content-type: text/plain');
   } else {
      // works for all 
      header('Content-type: application/' . $ext);extensions except txt
   }
   readfile($decrypted_file_path);
?>
 <p class="text-center">Download the Sample file <a href="<?php echo base_url();?>assets/csv/Sample-CSV-Format.txt">HERE</a> It has a sample of one entry</p>

This code is downloading the file on page load instead of link click. Also, it is downloading the whole html structure of the page I want only the text what I have written in text file.

Please guide where is the issue?

Upvotes: 5

Views: 8538

Answers (5)

Sonu Chohan
Sonu Chohan

Reputation: 273

public function getTxt()
{        
    $this->load->helper('download');  
    
    $dataFile       = "NOTE87";        
    $dataContent    = array();

    $dt = "Date :23/07/2021";

    $dataContent= array(
        "\n",
        "\t\t\tUTI AMC Limited\n",
        "\t\tDepartment of Fund Accounts\n",
        "\n",
        "\tReissue of Non Sale Remittance - Axis Bank Cases\n",
        "\n",
        "\t\t\t\tDate :".$dt."\n",
        "\n",
        
    );
    


    
    force_download($dataFile,implode($dataContent));
}

Upvotes: 0

Carlos Fdev
Carlos Fdev

Reputation: 755

You can do it like this, it won't redirect you and also works good for larger files.

In your controller "Controller.php"

function downloadFile(){
        $yourFile = "Sample-CSV-Format.txt";
        $file = @fopen($yourFile, "rb");

        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=TheNameYouWant.txt');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($yourFile));
        while (!feof($file)) {
            print(@fread($file, 1024 * 8));
            ob_flush();
            flush();
        }
}

In your view "view.php"

<a href="<?=base_url("Controller/downloadFile")?>">Download</a>

Upvotes: 3

souparno majumder
souparno majumder

Reputation: 2052

To my view its better to have the download code to the client side, than to have a controller-method written for this. you can use this ref

Upvotes: 0

Sopner Feriwala Niloy
Sopner Feriwala Niloy

Reputation: 123

You can do this simply in by HTML5 download atrribute . Just add this line in your downloading link .

<a href="<?php echo base_url();?>assets/csv/Sample-CSV-Format.txt" download="Sample-CSV-Format.txt"> HERE </a>

Upvotes: 7

Jpec
Jpec

Reputation: 300

make it like this

someother_file.php

<?php
   $file_name = "Sample-CSV-Format.txt";

   // extracting the extension:
   $ext = substr($file_name, strpos($file_name,'.')+1);

   header('Content-disposition: attachment; filename='.$file_name);

   if(strtolower($ext) == "txt")
   {
       header('Content-type: text/plain'); // works for txt only
   }
   else
   {
      header('Content-type: application/'.$ext); // works for all extensions except txt
    }
                                           readfile($decrypted_file_path);
 ?>

some_html_page.html

 <p class="text-center">Download the Sample file <a href="<?php echo base_url();?>/someother_file.php">HERE</a> It has a sample of one entry</p>

Upvotes: 0

Related Questions