0

hi master,
i have problem with my ci web, i use force_download in my controller but why redirect() cannot work, please tell me what my problem.

$filedown = $this->input->post('filedown');
            $datas = file_get_contents(base_url() . "down/" . $filedown); // Read the file's contents
            $name = "$filedown";

            force_download($name, $datas);

i have error, if i has wrong path file_get_content(path), can you tell me what my problem.

thank for your help

3
Contributors
4
Replies
23
Views
4 Years
Discussion Span
Last Post by Dani
0

Hi,
If the directory down/ is not directly accessible by web use $this->input->server('DOCUMENT_ROOT') instead of base_url().

Also, use file_exists() to be sure about the requested file and pathinfo() to grab the filename from $this->input->post('filedown'), otherwise a user can execute ../../../random_file:

$filedown = pathinfo($this->input->post('filedown'),PATHINFO_BASENAME);
$datas = $this->input->server('DOCUMENT_ROOT').'/down/'.$filedown;
if(file_exists($datas))
{
    force_download($filedown, $datas);
}
else
{
    // error action
}
0

Wow, you learn something new every day. I didn't know about force_download(). I was manually setting http headers and such for post attachments on DaniWeb that can't be viewed through a browser (Word documents, etc.)

0

Yup, it works fine. It's loaded by the download_helper.php in system/core/helpers.

A tip: when using $config['compress_output'] = TRUE add @ob_end_clean() above of force_download(). Otherwise, in some cases, it returns corrupted files.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.