• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HOW TO FORCE FILE DOWNLOD TO PC

#1
Sad 
Its been months now wrestling with download file in CI. When i try an example from documentation it works since its static but when i make it dynamic it doesn't work. Please take a look at this code and suggest the problem
http://pastebin.com/0jL4dj90
Reply

#2
"it doesn't work" does not give us much to go on. What did you expect and what did you get?
Reply

#3
(06-27-2016, 04:26 PM)PaulD Wrote: "it doesn't work" does not give us much to go on. What did you expect and what did you get?

Did you see the code by following the link i have given? I expect the file should be downloaded from the server to visitor's computer of which is not happening now.
Reply

#4
Well there could be a whole host of reasons. The main one is usually the file path. Try echoing out your filepath and filename and see if it is what you were expecting. I believe file_get_contents requires the full server path or a URL, not a relative path.

PS you may also want to add a check into your method that file_get_contents actually worked, and if not produce an error message.
Reply

#5
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
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#6
It seems "file_get_contents" has got a problem. So instead i tried this

$path = base_url('uploads/docs/'.$name); // Read the file's contents

The file is being downloaded to the pc properly but when i try to open it, its not opening. It becomes a corrupted file.

Why is this so and what could be the solution
Reply

#7
When downloading files, never refer to a url, but always to a path, relative to the folder where your index.php is.
The reason why the file seems to be downloaded, is that CI thinks it's a new file. So, it will download it, but the file won't have any content.
Reply

#8
(07-02-2016, 05:34 AM)Wouter60 Wrote: When downloading files, never refer to a url, but always to a path, relative to the folder where your index.php is.
The reason why the file seems to be downloaded, is that CI thinks it's a new file. So, it will download it, but the file won't have any content.

may you provide an example please..am stuck
Reply

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

#10
(07-01-2016, 03:37 PM)kwangu Wrote: It seems "file_get_contents" has got a problem. So instead i tried this

$path = base_url('uploads/docs/'.$name); // Read the file's contents

The file is being downloaded to the pc properly but when i try to open it, its not opening. It becomes a corrupted file.

Why is this so and what could be the solution

Read the file with a text editor.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.