Hi guys just a quick question...when you upload a file to the database and the file name is saved and encrypted in codeigniter how do i make a link that when clicked will download the file associated with the id of the table entry?

for instance this is my view file:

                        <?php foreach ($cvs as $row) {
                        ?>
                        <tr>
                            <td><?php echo $row['appID']; ?></td>
                            <td><?php echo $row['name']; ?></td>
                            <td><?php echo $row['email']; ?></td>
                            <td><?php echo $row['contact']; ?></td>
                            <td><?php echo Date::format($row['date_added']) ?></td>
                            <td><a href="<?php echo $row['cv']?>">Download CV</a></td>
                        </tr>
                        </tbody>
                        <?php } ?>

not all the code but that is the section i want the link to display as you can see it is showing a link but when clicked it does not download that file from the server how do i fix this. My framework is codeigniter 3.0

Thanks for your input

Riaan

Recommended Answers

All 3 Replies

Hi, you can create a method in your controller, search the database table and use the Download helper, for example:

public function download($id)
{
    # load download helper
    $this->load->helper('download');

    # search for filename by id
    $this->db->select('filename');
    $this->db->where('id', $id);
    $q = $this->db->get('tablename');

    # if exists continue
    if($q->num_rows() > 0)
    {
        $row  = $q->row();
        $file = FCPATH . 'files/'. $row->filename;
        if(file_exists($file))
            force_download($file, NULL);
    }

    else
        show_404();
}

Then simply create a link that points to the above method, for example:

http://site.tld/controller/download/123

Docs: http://www.codeigniter.com/user_guide/helpers/download_helper.html

helooo i have a some probleam for downloading a file from database. please help me how to download file from database and please send me the helper code for downloading...
thankyou

@mohammad

Hello, please provide more information (e.g. are you using CodeIgniter?) and your current code, also open your own thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.