Welcome Guest, Not a member yet? Register   Sign In
force_download problem
#1

[eluser]Whis[/eluser]
Hi,

I'm using the basic CI download helper: force_download, but it always keep displaying a blank page when I call the function...


Code:
public function getFile()
  {
    $data = file_get_contents(base_url()."/path/to/file.txt");
    $name= 'file.txt';
    force_download($name, $data);
  }


Any idea?

Thanks
#2

[eluser]TheFuzzy0ne[/eluser]
I'm not sure why you're got the call the base_url() in there. Using base_url() is probably causing the problem, as you're trying to open a URL with file_get_contents(). You can use:
Code:
$data = file_get_contents('path/to/file.txt);

to get the file contents.

If you really must get the contents of a URL, you can use stream_get_contents()
#3

[eluser]Whis[/eluser]
I just want to force the download of a file stored in a server when i click on a link. I also used this, but it didn't work too :

Code:
public function getFile()
  {
    $data = file_get_contents("/system/application/uploads/files/file.txt");
    $name= 'file.txt';
    force_download($name, $data);
  }

Thanks
#4

[eluser]pistolPete[/eluser]
Did you load the download helper?
Try using a relative path (use a period):
Code:
"./system/application/uploads/files/file.txt"
#5

[eluser]Whis[/eluser]
Thanks,

but the function just do nothing, I have a blank page without download.
The download helper is loaded and if I change the path I get an error ..

Don't understand why ...
#6

[eluser]Whis[/eluser]
Ok, I found the problem, Smile

I was testing the function with empty files... Why can't we download empty files ?

Thanks
#7

[eluser]TheFuzzy0ne[/eluser]
The second parameter of the force_download() function is required. If it's '', the function returns. After all, who would want an empty file? It's up to you to ensure that you actually have some data to send. Although I wouldn't recommend it, force_download() would probably work if you intentionally set the second argument to NULL. I haven't tested this however, but I still don't see the point in force_download()ing an empty file.




Theme © iAndrew 2016 - Forum software by © MyBB