![]() |
Download file only when click on link in foreach loop - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Download file only when click on link in foreach loop (/showthread.php?tid=67345) |
Download file only when click on link in foreach loop - wolfgang1983 - 02-12-2017 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"> Controller PHP Code: <?php RE: Download file only when click on link in foreach loop - Wouter60 - 02-12-2017 You're talking about a preview button. Where is it? I can't find it in your view. To force download of the links that are being displayed by the foreach loop, you need a slightly different approach. Replace the '<a> ... </a>' part with: PHP Code: <?php echo anchor('preview/downloadfile/' . $attachment['id'], $attachment['file_name']); ?> Instead of $attachment['id'], it may be even safer to work with something like $attachment['code'], which is a unique 32 character random string (see String Helper). That way, you neither reveal the entire path where your attachments are stored, nor the id of the attachments in your database. You will have to adjust the preview/downloadfile method to the new situation. It will get the id or code of the file as a parameter. Use that to look up the path in your database. Use that to actually force downloading the file. By the way, it's good practice to keep all your methods (in controllers) lower case only. On more tip: you can use the alternative php syntax in your views to improve readability: PHP Code: <?php foreach ($attachments as $attachment) : ?> RE: Download file only when click on link in foreach loop - wolfgang1983 - 02-13-2017 (02-12-2017, 11:55 PM)Wouter60 Wrote: You're talking about a preview button. Where is it? I can't find it in your view. On the view I have now changed the attachments to like and on my controller function I have added exit; is that OK. Seems to download now. PHP Code: <div class="row"> And on controller function PHP Code: public function downloads() { RE: Download file only when click on link in foreach loop - Wouter60 - 02-13-2017 Why uri_segment()? PHP Code: public function downloads($id) In your view, replace the '<a>...</a>' part with: PHP Code: <?php echo anchor('preview/downloads/' . $attachment['id'], $attachment['file_name');?> The anchor helper function accepts 2 or 3 parameters. 1 = the link to the controller/method (the function will convert this the actual url) 2 = the text to click on (can be an image) 3 = string with additional attributes, like class, title, style etc. |