Welcome Guest, Not a member yet? Register   Sign In
Download file only when click on link in foreach loop
#1

When I click on my preview button it downloads my file But I only want to be able to force download when I click on the path link in the foreach loop on view.

Question how can I force download when I click on a link in my foreach loop on view. Currently when I click on my preview button it downloads link which is wrong.

I have a attachments array on controller.

PHP Code:
<div class="panel panel-default">
<
div class="panel-heading"><h1 class="panel-title">Preview</h1></div>
<
div class="panel-body">
<
div class="row">
<
div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?
php echo $message;?>
</div>
</div>

<div class="row">
<?php foreach ($attachments as $attachment) {?>
<div class="col-lg-2 col-md-2 col-sm-2 col-xs-12">
<!-- Click link below to download.-->
<a class="<?php echo $attachment['path'];?>"><?php echo $attachment['file_name'];?></a>
</div>
<?php }?>
</div>

</div>
</div> 

Controller


PHP Code:
<?php

class Preview extends MX_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('parsedown');
        $this->parsedown->setLiteralBreaks(true);
        $this->load->helper('download');
    }

    public function index() {

        if ($this->input->post('newthread_preview')) {

            $data['message'] = $this->parsedown->text($this->input->post('message'));

            $data['attachments'] = array();

            $results $this->get_attachments($this->input->post('post_code'));

            if ($results) {

                foreach ($results as $result) {

                    $url base_url() . 'uploads/' $result['attachment_name'];
                    $path FCPATH 'uploads/' $result['attachment_name'];

                    $data['attachments'][] = array(
                        'file_name' => $result['file_name'],
                        'path' => $this->downloadFile($path)
                    );
                }

            }

            $this->load->view('template/forum/newthread_preview_view'$data);
        }
    }

    public function get_attachments($post_code) {
        $this->db->where('post_code'$post_code);
        $query $this->db->get($this->db->dbprefix 'attachments');

        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return FALSE;
        }
    }

    public function downloadFile($attachment_name) {
        return force_download($attachment_nameNULL);
    }



Attached Files
.php   Preview.php (Size: 1.28 KB / Downloads: 170)
.php   newthread_preview_view.php (Size: 500 bytes / Downloads: 156)
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply


Messages In This Thread
Download file only when click on link in foreach loop - by wolfgang1983 - 02-12-2017, 06:32 PM



Theme © iAndrew 2016 - Forum software by © MyBB