5

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?

eloibm
  • 899
  • 2
  • 11
  • 27
Geetika
  • 790
  • 3
  • 13
  • 29
  • Just move the download code in a different function and make the link point to that. Also, when you set the headers, anything that follows is also included in the download. That's why you need separate functions (one for the view, the other for the download). – Alex Tartan Jul 30 '15 at 06:54
  • you mean whatever written above the `

    ` tag needs to go in a controller function and the path to link will point that function?

    – Geetika Jul 30 '15 at 06:59
  • 1
    Yes. This might also help: http://code.runnable.com/Uha2YpCDyMQpAAJi/download-file-using-codeigniter – Alex Tartan Jul 30 '15 at 07:07
  • thanx give it a try.. – Geetika Jul 30 '15 at 07:11

5 Answers5

7

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>
2

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>
Mr. Pyramid
  • 3,855
  • 5
  • 32
  • 56
Carlos Fdev
  • 745
  • 6
  • 24
0

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>
Jpec
  • 300
  • 2
  • 14
0

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

Community
  • 1
  • 1
isnvi23h4
  • 1,910
  • 1
  • 27
  • 45
0
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));
}
Sonu Chohan
  • 141
  • 1
  • 5