0

I have the problem using the following code,I do not know what i did wrong. I want to download link for the file name from MySQL database

<tr>                        
<td>Employee Name</td>
<td>Project Name</td>
<td>Module Name</td>
<td>Task Name</td>
<td>File Attached</td>
</tr>

<?php
header("Content-type: plain text");
header("Content-Disposition: attachment; filename=file_attachment");
foreach($tel as $data){    
echo "<tr><td>"."<a href>".$data[file_attachment]."</td></tr>";
} ?>

when i load this page the output like download the HTML document of the page..... Please anyone suggest to me, how to achieve a download link for the stored text file from database.

2
  • create a text file in root directory and update the link in database. don't store files in database untill necessary
    – Sundar
    Oct 1, 2013 at 8:21
  • how to do update the link in db and how to download the file . will you give some sample... Oct 1, 2013 at 8:53

1 Answer 1

0

First of all: You have to header, before there is any output, so before you send any HTML. Second: You use HTML by purpose? I just ask, because you want to generate a textfile, I guess a CSV, so my anwer will assume this. So, what I would do is something like this:

<?php
    $output = 'Employee Name;Project Name;Module Name;Task Name;File Attached'; //The headline of the CSV to explain the entries
    $tel = array();
    foreach($tel as $data){
        $output .= PHP_EOL . ';' . $data["emp_name"] . ';' . $data["proj_name"] . ';' . $data["mod_name"] . ';' . $data["task_name"] . ';' . $data["file_attachment"];
    }

    $type = "text/csv";
    $size = strlen( $output );
    $filename = "table.csv";

    header("Content-type: " . $type);
    header("Content-length: " . $size);
    header("Content-Disposition: attachment; filename=" . $filename);
    header("Content-Description: PHP Generated Data");
    echo $output;
?>
2
  • Ah sorry, missunderstood the question. I would follow the comment of @Sundar. Save the file as a file in your webspace and save a link to this file in your DB. Oct 1, 2013 at 8:44
  • I already store the text file in db. my question is how to download the stored file from db. I dnt know my coding is right or wrong... please guide me right way.... thanks Oct 1, 2013 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.