本文整理汇总了PHP中FileHelper::getMimeTypeByExtension方法的典型用法代码示例。如果您正苦于以下问题:PHP FileHelper::getMimeTypeByExtension方法的具体用法?PHP FileHelper::getMimeTypeByExtension怎么用?PHP FileHelper::getMimeTypeByExtension使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FileHelper
的用法示例。
在下文中一共展示了FileHelper::getMimeTypeByExtension方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: sendFile
/**
* Sends a file to the user.
*
* We’re overriding this from {@link \CHttpRequest::sendFile()} so we can have more control over the headers.
*
* @param string $path The path to the file on the server.
* @param string $content The contents of the file.
* @param array|null $options An array of optional options. Possible keys include 'forceDownload', 'mimeType',
* and 'cache'.
* @param bool|null $terminate Whether the request should be terminated after the file has been sent.
* Defaults to `true`.
*
* @throws HttpException
* @return null
*/
public function sendFile($path, $content, $options = array(), $terminate = true)
{
$fileName = IOHelper::getFileName($path, true);
// Clear the output buffer to prevent corrupt downloads. Need to check the OB status first, or else some PHP
// versions will throw an E_NOTICE since we have a custom error handler
// (http://pear.php.net/bugs/bug.php?id=9670)
if (ob_get_length() !== false) {
// If zlib.output_compression is enabled, then ob_clean() will corrupt the results of output buffering.
// ob_end_clean is what we want.
ob_end_clean();
}
// Default to disposition to 'download'
$forceDownload = !isset($options['forceDownload']) || $options['forceDownload'];
if ($forceDownload) {
HeaderHelper::setDownload($fileName);
}
if (empty($options['mimeType'])) {
if (($options['mimeType'] = FileHelper::getMimeTypeByExtension($fileName)) === null) {
$options['mimeType'] = 'text/plain';
}
}
HeaderHelper::setHeader(array('Content-Type' => $options['mimeType'] . '; charset=utf-8'));
$fileSize = mb_strlen($content, '8bit');
$contentStart = 0;
$contentEnd = $fileSize - 1;
$httpVersion = $this->getHttpVersion();
if (isset($_SERVER['HTTP_RANGE'])) {
HeaderHelper::setHeader(array('Accept-Ranges' => 'bytes'));
// Client sent us a multibyte range, can not hold this one for now
if (mb_strpos($_SERVER['HTTP_RANGE'], ',') !== false) {
HeaderHelper::setHeader(array('Content-Range' => 'bytes ' . $contentStart - $contentEnd / $fileSize));
throw new HttpException(416, 'Requested Range Not Satisfiable');
}
$range = str_replace('bytes=', '', $_SERVER['HTTP_RANGE']);
// range requests starts from "-", so it means that data must be dumped the end point.
if ($range[0] === '-') {
$contentStart = $fileSize - mb_substr($range, 1);
} else {
$range = explode('-', $range);
$contentStart = $range[0];
// check if the last-byte-pos presents in header
if (isset($range[1]) && is_numeric($range[1])) {
$contentEnd = $range[1];
}
}
// Check the range and make sure it's treated according to the specs.
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
// End bytes can not be larger than $end.
$contentEnd = $contentEnd > $fileSize ? $fileSize - 1 : $contentEnd;
// Validate the requested range and return an error if it's not correct.
$wrongContentStart = $contentStart > $contentEnd || $contentStart > $fileSize - 1 || $contentStart < 0;
if ($wrongContentStart) {
HeaderHelper::setHeader(array('Content-Range' => 'bytes ' . $contentStart - $contentEnd / $fileSize));
throw new HttpException(416, 'Requested Range Not Satisfiable');
}
HeaderHelper::setHeader("HTTP/{$httpVersion} 206 Partial Content");
HeaderHelper::setHeader(array('Content-Range' => 'bytes ' . $contentStart - $contentEnd / $fileSize));
} else {
HeaderHelper::setHeader("HTTP/{$httpVersion} 200 OK");
}
// Calculate new content length
$length = $contentEnd - $contentStart + 1;
if (!empty($options['cache'])) {
$cacheTime = 31536000;
// 1 year
HeaderHelper::setHeader(array('Expires' => gmdate('D, d M Y H:i:s', time() + $cacheTime) . ' GMT'));
HeaderHelper::setHeader(array('Pragma' => 'cache'));
HeaderHelper::setHeader(array('Cache-Control' => 'max-age=' . $cacheTime));
$modifiedTime = IOHelper::getLastTimeModified($path);
HeaderHelper::setHeader(array('Last-Modified' => gmdate("D, d M Y H:i:s", $modifiedTime->getTimestamp()) . ' GMT'));
} else {
if (!$forceDownload) {
HeaderHelper::setNoCache();
} else {
// Fixes a bug in IE 6, 7 and 8 when trying to force download a file over SSL:
// https://stackoverflow.com/questions/1218925/php-script-to-download-file-not-working-in-ie
HeaderHelper::setHeader(array('Pragma' => '', 'Cache-Control' => ''));
}
}
if ($options['mimeType'] == 'application/x-javascript' || $options['mimeType'] == 'text/css') {
HeaderHelper::setHeader(array('Vary' => 'Accept-Encoding'));
}
if (!ob_get_length()) {
HeaderHelper::setLength($length);
}
//.........这里部分代码省略.........
示例2: getMimeTypeByExtension
/**
* A wrapper for {@link FileHelper::getMimeTypeByExtension()}.
*
* @param string $path The path to test.
*
* @return string The mime type.
*/
public static function getMimeTypeByExtension($path)
{
return FileHelper::getMimeTypeByExtension($path);
}