'CodeIgniter force_download is not working
CodeIgniter force_download
is not working
My Code:
if(!empty($articlefile)){
if(file_exists('./media/journals/xml/'.$articlefile)){
$this->load->helper('download');
$articlefile = str_replace(" ", "%20", $articlefile);
$path=file_get_contents(getBaseUrl().'media/journals/xml/'.$articlefile);
force_download($articlefile,$path);
}else{
redirect(getBaseUrl());
exit();
}
}
The same code was working fine before and today this code is not working I don't know why? I am using the PHP 7.4 version but still getting errors.
Solution 1:[1]
From looking at your code, I am wondering what the $path variable contains right before the force_download call is issued. For example, if there is in error such that $path = null, then force_download will fail because $articleFile is not a full path. It appears from your code that the article file is on the local file system, and I am questioning your usage of getBaseUrl in the file_get_contents call.
I would also try a basic example to see if your routing and control structures are working as expected.
Using the example from the force_download page in the manual in conjunction with your code I might try:
if(!empty($articlefile)){
if(file_exists('./media/journals/xml/'.$articlefile)){
$this->load->helper('download');
//$articlefile = str_replace(" ", "%20", $articlefile);
//$path=file_get_contents(getBaseUrl().'media/journals/xml/'.$articlefile);
//force_download($articlefile,$path);
$data = 'Here is some text!';
$name = 'mytext.txt';
force_download($name, $data);
}
else{
redirect(getBaseUrl());
exit();
}
}
If that works, you can separately try your name and data variables to narrow down which one is not performing as expected.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | colonelclick |