CodeIgniter Forums
HOW TO FORCE FILE DOWNLOD TO PC - 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: HOW TO FORCE FILE DOWNLOD TO PC (/showthread.php?tid=65577)

Pages: 1 2


RE: HOW TO FORCE FILE DOWNLOD TO PC - PaulD - 07-03-2016

Presuming you are downloading existing files, why are you reading in the contents and then downloading the file. You could just give the file path and force the download, and the browser will handle it in whatever way it wants. If your file is not corrupted on the server it will be fine after download too. Perhaps reading the file in as a string is losing some important parts of the file code in the translation to a string.


RE: HOW TO FORCE FILE DOWNLOD TO PC - kwangu - 07-11-2016

May you please give an example. that would be much better


RE: HOW TO FORCE FILE DOWNLOD TO PC - InsiteFX - 07-11-2016

Here is a us government top secret link give out at your own risk!




RE: HOW TO FORCE FILE DOWNLOD TO PC - kwangu - 07-17-2016

(06-29-2016, 06:07 PM)InsiteFX Wrote: Try this and see if it works for you.

PHP Code:
$path '/uploads/docs/'.$name// set the file path

force_download($pathnulltrue); 

null - will read the contents from the path
true - will force the mime type to the filename

I have tried but its not working


RE: HOW TO FORCE FILE DOWNLOD TO PC - InsiteFX - 07-17-2016

Here is a link that may explain it to you better.

Force File Download


RE: HOW TO FORCE FILE DOWNLOD TO PC - kwangu - 07-17-2016

(07-02-2016, 11:21 PM)Wouter60 Wrote: First, make sure that you don't pass the filename directly to your controller, because it becomes part of the url.
The link in your view should be like this:
PHP Code:
echo anchor('download/download_file/' urlencode($filename),$filename);
//the anchor function is in the url helper 

Now, in the controller method, I decode the given filename.
PHP Code:
public function download_file($name)
{
 
     //$name is the encoded filename, passed by the view
 
     $this->load->helper('download');
 
     $path './uploads/docs/' urldecode($name);
 
     if (file_exists($path)) {
 
        force_download($path,NULL);
 
     }
 
     else {
 
        show_error('File not found!');
 
        die();
 
     }

Let me know if this helps.

I have tried the code but still its not working. Am using CI 2.2.6 When i click on the link it redirects to black page and the this "http://codeigniter/index.php/download/download_file/january2013.pdf" is what is the url, the file is not downloaded