Qiang Xue
12 years ago
8 changed files with 881 additions and 510 deletions
@ -0,0 +1,187 @@
|
||||
<?php |
||||
/** |
||||
* MIME types. |
||||
* |
||||
* This file contains most commonly used MIME types |
||||
* according to file extension names. |
||||
* |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright © 2008-2012 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
* @since 2.0 |
||||
*/ |
||||
|
||||
return array( |
||||
'ai' => 'application/postscript', |
||||
'aif' => 'audio/x-aiff', |
||||
'aifc' => 'audio/x-aiff', |
||||
'aiff' => 'audio/x-aiff', |
||||
'anx' => 'application/annodex', |
||||
'asc' => 'text/plain', |
||||
'au' => 'audio/basic', |
||||
'avi' => 'video/x-msvideo', |
||||
'axa' => 'audio/annodex', |
||||
'axv' => 'video/annodex', |
||||
'bcpio' => 'application/x-bcpio', |
||||
'bin' => 'application/octet-stream', |
||||
'bmp' => 'image/bmp', |
||||
'c' => 'text/plain', |
||||
'cc' => 'text/plain', |
||||
'ccad' => 'application/clariscad', |
||||
'cdf' => 'application/x-netcdf', |
||||
'class' => 'application/octet-stream', |
||||
'cpio' => 'application/x-cpio', |
||||
'cpt' => 'application/mac-compactpro', |
||||
'csh' => 'application/x-csh', |
||||
'css' => 'text/css', |
||||
'dcr' => 'application/x-director', |
||||
'dir' => 'application/x-director', |
||||
'dms' => 'application/octet-stream', |
||||
'doc' => 'application/msword', |
||||
'drw' => 'application/drafting', |
||||
'dvi' => 'application/x-dvi', |
||||
'dwg' => 'application/acad', |
||||
'dxf' => 'application/dxf', |
||||
'dxr' => 'application/x-director', |
||||
'eps' => 'application/postscript', |
||||
'etx' => 'text/x-setext', |
||||
'exe' => 'application/octet-stream', |
||||
'ez' => 'application/andrew-inset', |
||||
'f' => 'text/plain', |
||||
'f90' => 'text/plain', |
||||
'flac' => 'audio/flac', |
||||
'fli' => 'video/x-fli', |
||||
'flv' => 'video/x-flv', |
||||
'gif' => 'image/gif', |
||||
'gtar' => 'application/x-gtar', |
||||
'gz' => 'application/x-gzip', |
||||
'h' => 'text/plain', |
||||
'hdf' => 'application/x-hdf', |
||||
'hh' => 'text/plain', |
||||
'hqx' => 'application/mac-binhex40', |
||||
'htm' => 'text/html', |
||||
'html' => 'text/html', |
||||
'ice' => 'x-conference/x-cooltalk', |
||||
'ief' => 'image/ief', |
||||
'iges' => 'model/iges', |
||||
'igs' => 'model/iges', |
||||
'ips' => 'application/x-ipscript', |
||||
'ipx' => 'application/x-ipix', |
||||
'jpe' => 'image/jpeg', |
||||
'jpeg' => 'image/jpeg', |
||||
'jpg' => 'image/jpeg', |
||||
'js' => 'application/x-javascript', |
||||
'kar' => 'audio/midi', |
||||
'latex' => 'application/x-latex', |
||||
'lha' => 'application/octet-stream', |
||||
'lsp' => 'application/x-lisp', |
||||
'lzh' => 'application/octet-stream', |
||||
'm' => 'text/plain', |
||||
'man' => 'application/x-troff-man', |
||||
'me' => 'application/x-troff-me', |
||||
'mesh' => 'model/mesh', |
||||
'mid' => 'audio/midi', |
||||
'midi' => 'audio/midi', |
||||
'mif' => 'application/vnd.mif', |
||||
'mime' => 'www/mime', |
||||
'mov' => 'video/quicktime', |
||||
'movie' => 'video/x-sgi-movie', |
||||
'mp2' => 'audio/mpeg', |
||||
'mp3' => 'audio/mpeg', |
||||
'mpe' => 'video/mpeg', |
||||
'mpeg' => 'video/mpeg', |
||||
'mpg' => 'video/mpeg', |
||||
'mpga' => 'audio/mpeg', |
||||
'ms' => 'application/x-troff-ms', |
||||
'msh' => 'model/mesh', |
||||
'nc' => 'application/x-netcdf', |
||||
'oga' => 'audio/ogg', |
||||
'ogg' => 'audio/ogg', |
||||
'ogv' => 'video/ogg', |
||||
'ogx' => 'application/ogg', |
||||
'oda' => 'application/oda', |
||||
'pbm' => 'image/x-portable-bitmap', |
||||
'pdb' => 'chemical/x-pdb', |
||||
'pdf' => 'application/pdf', |
||||
'pgm' => 'image/x-portable-graymap', |
||||
'pgn' => 'application/x-chess-pgn', |
||||
'png' => 'image/png', |
||||
'pnm' => 'image/x-portable-anymap', |
||||
'pot' => 'application/mspowerpoint', |
||||
'ppm' => 'image/x-portable-pixmap', |
||||
'pps' => 'application/mspowerpoint', |
||||
'ppt' => 'application/mspowerpoint', |
||||
'ppz' => 'application/mspowerpoint', |
||||
'pre' => 'application/x-freelance', |
||||
'prt' => 'application/pro_eng', |
||||
'ps' => 'application/postscript', |
||||
'qt' => 'video/quicktime', |
||||
'ra' => 'audio/x-realaudio', |
||||
'ram' => 'audio/x-pn-realaudio', |
||||
'ras' => 'image/cmu-raster', |
||||
'rgb' => 'image/x-rgb', |
||||
'rm' => 'audio/x-pn-realaudio', |
||||
'roff' => 'application/x-troff', |
||||
'rpm' => 'audio/x-pn-realaudio-plugin', |
||||
'rtf' => 'text/rtf', |
||||
'rtx' => 'text/richtext', |
||||
'scm' => 'application/x-lotusscreencam', |
||||
'set' => 'application/set', |
||||
'sgm' => 'text/sgml', |
||||
'sgml' => 'text/sgml', |
||||
'sh' => 'application/x-sh', |
||||
'shar' => 'application/x-shar', |
||||
'silo' => 'model/mesh', |
||||
'sit' => 'application/x-stuffit', |
||||
'skd' => 'application/x-koan', |
||||
'skm' => 'application/x-koan', |
||||
'skp' => 'application/x-koan', |
||||
'skt' => 'application/x-koan', |
||||
'smi' => 'application/smil', |
||||
'smil' => 'application/smil', |
||||
'snd' => 'audio/basic', |
||||
'sol' => 'application/solids', |
||||
'spl' => 'application/x-futuresplash', |
||||
'spx' => 'audio/ogg', |
||||
'src' => 'application/x-wais-source', |
||||
'step' => 'application/STEP', |
||||
'stl' => 'application/SLA', |
||||
'stp' => 'application/STEP', |
||||
'sv4cpio' => 'application/x-sv4cpio', |
||||
'sv4crc' => 'application/x-sv4crc', |
||||
'swf' => 'application/x-shockwave-flash', |
||||
't' => 'application/x-troff', |
||||
'tar' => 'application/x-tar', |
||||
'tcl' => 'application/x-tcl', |
||||
'tex' => 'application/x-tex', |
||||
'texi' => 'application/x-texinfo', |
||||
'texinfo' => 'application/x-texinfo', |
||||
'tif' => 'image/tiff', |
||||
'tiff' => 'image/tiff', |
||||
'tr' => 'application/x-troff', |
||||
'tsi' => 'audio/TSP-audio', |
||||
'tsp' => 'application/dsptype', |
||||
'tsv' => 'text/tab-separated-values', |
||||
'txt' => 'text/plain', |
||||
'unv' => 'application/i-deas', |
||||
'ustar' => 'application/x-ustar', |
||||
'vcd' => 'application/x-cdlink', |
||||
'vda' => 'application/vda', |
||||
'viv' => 'video/vnd.vivo', |
||||
'vivo' => 'video/vnd.vivo', |
||||
'vrml' => 'model/vrml', |
||||
'wav' => 'audio/x-wav', |
||||
'wrl' => 'model/vrml', |
||||
'xbm' => 'image/x-xbitmap', |
||||
'xlc' => 'application/vnd.ms-excel', |
||||
'xll' => 'application/vnd.ms-excel', |
||||
'xlm' => 'application/vnd.ms-excel', |
||||
'xls' => 'application/vnd.ms-excel', |
||||
'xlw' => 'application/vnd.ms-excel', |
||||
'xml' => 'application/xml', |
||||
'xpm' => 'image/x-xpixmap', |
||||
'xspf' => 'application/xspf+xml', |
||||
'xwd' => 'image/x-xwindowdump', |
||||
'xyz' => 'chemical/x-pdb', |
||||
'zip' => 'application/zip', |
||||
); |
@ -0,0 +1,144 @@
|
||||
<?php |
||||
/** |
||||
* Response class file. |
||||
* |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright © 2008-2012 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\web; |
||||
|
||||
use yii\util\FileHelper; |
||||
|
||||
/** |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class Response extends \yii\base\Response |
||||
{ |
||||
/** |
||||
* Sends a file to user. |
||||
* @param string $fileName file name |
||||
* @param string $content content to be set. |
||||
* @param string $mimeType mime type of the content. If null, it will be guessed automatically based on the given file name. |
||||
* @param boolean $terminate whether to terminate the current application after calling this method |
||||
* @todo |
||||
*/ |
||||
public function sendFile($fileName, $content, $mimeType = null, $terminate = true) |
||||
{ |
||||
if ($mimeType === null && ($mimeType = FileHelper::getMimeType($fileName)) === null) { |
||||
$mimeType = 'application/octet-stream'; |
||||
} |
||||
header('Pragma: public'); |
||||
header('Expires: 0'); |
||||
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
||||
header("Content-type: $mimeType"); |
||||
if (ob_get_length() === false) { |
||||
header('Content-Length: ' . (function_exists('mb_strlen') ? mb_strlen($content, '8bit') : strlen($content))); |
||||
} |
||||
header("Content-Disposition: attachment; filename=\"$fileName\""); |
||||
header('Content-Transfer-Encoding: binary'); |
||||
|
||||
if ($terminate) { |
||||
// clean up the application first because the file downloading could take long time |
||||
// which may cause timeout of some resources (such as DB connection) |
||||
Yii::app()->end(0, false); |
||||
echo $content; |
||||
exit(0); |
||||
} else { |
||||
echo $content; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Sends existing file to a browser as a download using x-sendfile. |
||||
* |
||||
* X-Sendfile is a feature allowing a web application to redirect the request for a file to the webserver |
||||
* that in turn processes the request, this way eliminating the need to perform tasks like reading the file |
||||
* and sending it to the user. When dealing with a lot of files (or very big files) this can lead to a great |
||||
* increase in performance as the web application is allowed to terminate earlier while the webserver is |
||||
* handling the request. |
||||
* |
||||
* The request is sent to the server through a special non-standard HTTP-header. |
||||
* When the web server encounters the presence of such header it will discard all output and send the file |
||||
* specified by that header using web server internals including all optimizations like caching-headers. |
||||
* |
||||
* As this header directive is non-standard different directives exists for different web servers applications: |
||||
* <ul> |
||||
* <li>Apache: {@link http://tn123.org/mod_xsendfile X-Sendfile}</li> |
||||
* <li>Lighttpd v1.4: {@link http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file X-LIGHTTPD-send-file}</li> |
||||
* <li>Lighttpd v1.5: {@link http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file X-Sendfile}</li> |
||||
* <li>Nginx: {@link http://wiki.nginx.org/XSendfile X-Accel-Redirect}</li> |
||||
* <li>Cherokee: {@link http://www.cherokee-project.com/doc/other_goodies.html#x-sendfile X-Sendfile and X-Accel-Redirect}</li> |
||||
* </ul> |
||||
* So for this method to work the X-SENDFILE option/module should be enabled by the web server and |
||||
* a proper xHeader should be sent. |
||||
* |
||||
* <b>Note:</b> |
||||
* This option allows to download files that are not under web folders, and even files that are otherwise protected (deny from all) like .htaccess |
||||
* |
||||
* <b>Side effects</b>: |
||||
* If this option is disabled by the web server, when this method is called a download configuration dialog |
||||
* will open but the downloaded file will have 0 bytes. |
||||
* |
||||
* <b>Example</b>: |
||||
* <pre> |
||||
* <?php |
||||
* Yii::app()->request->xSendFile('/home/user/Pictures/picture1.jpg',array( |
||||
* 'saveName'=>'image1.jpg', |
||||
* 'mimeType'=>'image/jpeg', |
||||
* 'terminate'=>false, |
||||
* )); |
||||
* ?> |
||||
* </pre> |
||||
* @param string $filePath file name with full path |
||||
* @param array $options additional options: |
||||
* <ul> |
||||
* <li>saveName: file name shown to the user, if not set real file name will be used</li> |
||||
* <li>mimeType: mime type of the file, if not set it will be guessed automatically based on the file name, if set to null no content-type header will be sent.</li> |
||||
* <li>xHeader: appropriate x-sendfile header, defaults to "X-Sendfile"</li> |
||||
* <li>terminate: whether to terminate the current application after calling this method, defaults to true</li> |
||||
* <li>forceDownload: specifies whether the file will be downloaded or shown inline, defaults to true. (Since version 1.1.9.)</li> |
||||
* <li>addHeaders: an array of additional http headers in header-value pairs (available since version 1.1.10)</li> |
||||
* </ul> |
||||
* @todo |
||||
*/ |
||||
public function xSendFile($filePath, $options = array()) |
||||
{ |
||||
if (!isset($options['forceDownload']) || $options['forceDownload']) { |
||||
$disposition = 'attachment'; |
||||
} else { |
||||
$disposition = 'inline'; |
||||
} |
||||
|
||||
if (!isset($options['saveName'])) { |
||||
$options['saveName'] = basename($filePath); |
||||
} |
||||
|
||||
if (!isset($options['mimeType'])) { |
||||
if (($options['mimeType'] = CFileHelper::getMimeTypeByExtension($filePath)) === null) { |
||||
$options['mimeType'] = 'text/plain'; |
||||
} |
||||
} |
||||
|
||||
if (!isset($options['xHeader'])) { |
||||
$options['xHeader'] = 'X-Sendfile'; |
||||
} |
||||
|
||||
if ($options['mimeType'] !== null) { |
||||
header('Content-type: ' . $options['mimeType']); |
||||
} |
||||
header('Content-Disposition: ' . $disposition . '; filename="' . $options['saveName'] . '"'); |
||||
if (isset($options['addHeaders'])) { |
||||
foreach ($options['addHeaders'] as $header => $value) { |
||||
header($header . ': ' . $value); |
||||
} |
||||
} |
||||
header(trim($options['xHeader']) . ': ' . $filePath); |
||||
|
||||
if (!isset($options['terminate']) || $options['terminate']) { |
||||
Yii::app()->end(); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue