Question

I'm trying to send a file to the browser to download, and not having a luck using $this->response->send_file($file_path); in the controller.

I get the following error:

ErrorException [ Warning ]: finfo::file() [<a href='finfo.file'>finfo.file</a>]: Empty filename or path

$file_path can be either absolute or relative path, but I still get the same error. After looking at the Kohana code for this implementation I just can't figure out how this should work.

The following code will shows how a base filename (eg, filename.ext) is passed into File::mime() - which is wrong

https://github.com/kohana/core/blob/3.2/develop/classes/kohana/response.php#L434-453

// Get the complete file path
$filename = realpath($filename);

if (empty($download))
{
    // Use the file name as the download file name
    $download = pathinfo($filename, PATHINFO_BASENAME);
}

// Get the file size
$size = filesize($filename);

if ( ! isset($mime))
{
    // Get the mime type
    // HERE'S THE ISSUE!!!
    $mime = File::mime($download);
}

File::mime expects the filepath to be absolute or relative path on the filesystem, but $download will only ever be a base filename (eg filename.ext);

The only solution that works for me right now is to change the code in the send_file() method 'classes/kohana/response.php'

from File::mime($download); to $mime = File::mime($filename);.

Kohana 3.3 has changed this implementation to:

$mime = File::mime_by_ext(pathinfo($download, PATHINFO_EXTENSION));

Essentially send_file does not work in 3.2 without this fix. Is this a bug, or what am I missing here?

Was it helpful?

Solution

I was using and linking to the 3.2 develop branch. This issue does not exist in the 3.2 master branch.

For those interested, follow discussion on this pull request to view the eventual fix: https://github.com/kohana/core/pull/183

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top