Headers for file download the CI way |
[eluser]mr_prasanna[/eluser]
Hi I tried the set output header method to do the following but had no luck. Browsers displayed the binary stream instead of forcing to download. What's the most graceful way to achieve this in CI? This snippet is part of 'download' function in a controller: Code: header('Content-Description: File Transfer');
[eluser]flaky[/eluser]
http://ellislab.com/codeigniter/user-gui...elper.html
[eluser]mr_prasanna[/eluser]
What scared me in this method is, what if the file size is like 50MB, 80+MB or 1GB? Will it still work fine and efficient than any other alternative PHP has to offer? Code: $data = file_get_contents("/path/to/photo.jpg"); // Read the file's contents
[eluser]flaky[/eluser]
As you can see from the code below (download_helper.php), CodeIgniter uses php to set the headers, it doesn't have it's own set of complicated functions, but basically uses php's functions. Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
[eluser]mr_prasanna[/eluser]
Ok, what if we change this and leave everything else as is. Don't you think it might be better instead of getting a large file's content into PHP variable? Code: function force_download($filename = '', $data = '') To Code: function force_download($filename = '', $file_full_path = '') and Code: exit($data); To Code: readfile($file_full_path);
[eluser]danmontgomery[/eluser]
Accomplishes exactly the same thing... The only difference would be that it limits functionality to files that already exist on the filesystem, so you couldn't dynamically generate a file and have the user download it... If it suits your needs, there's no reason not to do it.
[eluser]mr_prasanna[/eluser]
Awesome! So, I think I should customize the helper to do both what it is doing now and a way to just accept filename and do the job which should be quick and doesn't require to pass a large copy of a variable from object to object. Thanks. |
Welcome Guest, Not a member yet? Register Sign In |