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

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) : ?>
   //...
<?php endforeach; ?>
Reply


Messages In This Thread
RE: Download file only when click on link in foreach loop - by Wouter60 - 02-12-2017, 11:55 PM



Theme © iAndrew 2016 - Forum software by © MyBB