Jeeva
Jeeva

Reputation: 642

Why are getting contents from the file when we just have to Download in Codeigniter

Here is a Simple code where i need to download a content from files ..but i don't understand why are we getting contents to a variable , why don't we just give link to download and force it to download.

<?php   
   if(file_exists("assets/uploads/file.csv")) {                                   
   $url = $BASE_URL.'assets/uploads/file.csv';
   $data = file_get_contents($url); // Read the file's contents
?>
<a href="<?php echo $BASE_URL.'assets/uploads/file.csv'; ?>" onclick="<?php force_download($data); ?>">Download</a>

Upvotes: 0

Views: 37

Answers (1)

Bob Brinks
Bob Brinks

Reputation: 1392

You seem to be confusing downloading to the server side with downloading to the client side (browser).

To download the csv file to the browser you have to serve it using a http server (apache or something like that) and then link to the url of the file on the server. (Like you are doing in the a href= ).

On the server side you don't have to do any file_get_contents or load the data of this file. That is all handled by apache.

Upvotes: 1

Related Questions