Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 270
Geetika Avatar asked Jul 30 '15 06:07

Geetika


1 Answers

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>
like image 83
Sopner Feriwala Niloy Avatar answered Oct 26 '22 23:10

Sopner Feriwala Niloy