HOW TO FORCE FILE DOWNLOD TO PC |
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
"it doesn't work" does not give us much to go on. What did you expect and what did you get?
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.
Try this and see if it works for you.
PHP Code: $path = '/uploads/docs/'.$name; // set the file path 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 )
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
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. (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. may you provide an example please..am stuck
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); Now, in the controller method, I decode the given filename. PHP Code: public function download_file($name)
(07-01-2016, 03:37 PM)kwangu Wrote: It seems "file_get_contents" has got a problem. So instead i tried this Read the file with a text editor. |
Welcome Guest, Not a member yet? Register Sign In |