1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
3 * Download helper class.
5 * $Id: download.php 3917 2009-01-21 03:06:22Z zombor $
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
15 * Force a download of a file to the user's browser. This function is
16 * binary-safe and will work with any MIME type that Kohana is aware of.
18 * @param string a file path or file name
19 * @param mixed data to be sent if the filename does not exist
20 * @param string suggested filename to display in the download
23 public static function force($filename = NULL, $data = NULL, $nicename = NULL)
28 if (is_file($filename))
31 $filepath = str_replace('\\', '/', realpath($filename));
34 $filesize = filesize($filepath);
37 $filename = substr(strrchr('/'.$filepath, '/'), 1);
40 $extension = strtolower(substr(strrchr($filepath, '.'), 1));
45 $filesize = strlen($data);
47 // Make sure the filename does not have directory info
48 $filename = substr(strrchr('/'.$filename, '/'), 1);
51 $extension = strtolower(substr(strrchr($filename, '.'), 1));
54 // Get the mime type of the file
55 $mime = Kohana
::config('mimes.'.$extension);
59 // Set a default mime if none was found
60 $mime = array('application/octet-stream');
63 // Generate the server headers
64 header('Content-Type: '.$mime[0]);
65 header('Content-Disposition: attachment; filename="'.(empty($nicename) ?
$filename : $nicename).'"');
66 header('Content-Transfer-Encoding: binary');
67 header('Content-Length: '.sprintf('%d', $filesize));
69 // More caching prevention
72 if (Kohana
::user_agent('browser') === 'Internet Explorer')
75 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
76 header('Pragma: public');
80 // Send normal headers
81 header('Pragma: no-cache');
84 // Clear the output buffer
85 Kohana
::close_buffers(FALSE);
90 $handle = fopen($filepath, 'rb');
100 // Send the file data