Welcome Guest, Not a member yet? Register   Sign In
HOW TO FORCE FILE DOWNLOD TO PC
#11

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.
Reply
#12

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

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

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#14

(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
Reply
#15

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

Force File Download
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#16

(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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB