Skip to content

Commit

Permalink
Merge pull request #232 from leonar15/devel
Browse files Browse the repository at this point in the history
allows Response::send_file() to correctly send file resources
  • Loading branch information
WinterSilence committed May 20, 2018
2 parents d9b826a + 1de4fb3 commit 08c1cfa
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion system/classes/Kohana/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,14 +382,20 @@ public function send_headers($replace = FALSE, $callback = NULL)
*
* $request->send_file('media/packages/kohana.zip');
*
* Download a generated file:
*
* $csv = tmpfile();
* fputcsv($csv, ['label1', 'label2']);
* $request->send_file($csv, $filename);
*
* Download generated content as a file:
*
* $request->response($content);
* $request->send_file(TRUE, $filename);
*
* [!!] No further processing can be done after this method is called!
*
* @param string $filename filename with path, or TRUE for the current response
* @param string|resource|bool $filename filename with path, file stream, or TRUE for the current response
* @param string $download downloaded file name
* @param array $options additional options
* @return void
Expand Down Expand Up @@ -437,6 +443,24 @@ public function send_file($filename, $download = NULL, array $options = NULL)
// File data is no longer needed
unset($file_data);
}
else if (is_resource($filename) && get_resource_type($filename) === 'stream')
{
if (empty($download))
{
throw new Kohana_Exception('Download name must be provided for streaming files');
}

// Make sure this is a file handle
$file_meta = stream_get_meta_data($filename);
if ($file_meta['seekable'] === FALSE)
{
throw new Kohana_Exception('Resource must be a file handle');
}

// Handle file streams passed in as resources
$file = $filename;
$size = fstat($file)['size'];
}
else
{
// Get the complete file path
Expand Down

0 comments on commit 08c1cfa

Please sign in to comment.