本文整理汇总了PHP中File::mime_by_ext方法的典型用法代码示例。如果您正苦于以下问题:PHP File::mime_by_ext方法的具体用法?PHP File::mime_by_ext怎么用?PHP File::mime_by_ext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类File
的用法示例。
在下文中一共展示了File::mime_by_ext方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: action_index
/**
* Serve the file to the browser AND cache it for direct access if in STAGING OR PRODUCTION.
*/
public function action_index()
{
$file = $this->request->param('file');
$ext = pathinfo($file, PATHINFO_EXTENSION);
$path = Kohana::find_file('assets', $file, FALSE);
if ($path === FALSE) {
throw HTTP_Exception::factory('404', 'File not found!');
}
$dir = DOCROOT . 'assets' . DIRECTORY_SEPARATOR;
// Set the proper headers for browser caching
$this->response->headers('content-type', File::mime_by_ext($ext));
$this->response->headers('last-modified', date('r', filemtime($path)));
$content = file_get_contents($path);
$this->response->body($content);
// Don't cache the assets unless we are in STAGING OR PRODUCTION.
if (Kohana::$environment >= Kohana::STAGING) {
return;
}
// Only cache for specific extensions.
if (!in_array($ext, $this->_cache_extensions)) {
return;
}
// Check if assets sub dir exist.
$parts = explode('/', $file);
$file = array_pop($parts);
foreach ($parts as $part) {
$dir .= $part . DIRECTORY_SEPARATOR;
if (!is_dir($dir)) {
mkdir($dir);
}
}
file_put_contents($dir . $file, $content);
}
示例2: action_index
public function action_index()
{
$group = $this->request->param('group');
if (!$group) {
throw new HTTP_Exception_404();
}
$config = Kohana::$config->load('thumb')->get($group);
if (!$config) {
throw new HTTP_Exception_404();
}
$file = $this->request->param('file');
if (!$file) {
throw new HTTP_Exception_404();
}
$path = Arr::get($config, 'path', '');
if (!empty($path)) {
$path = rtrim($path, '/') . '/';
}
$thumb = Thumb::create($group, $path . $file);
if ($thumb) {
// Get the extension from the filename
$ext = strtolower(pathinfo($thumb, PATHINFO_EXTENSION));
$mime = File::mime_by_ext($ext);
$this->response->headers('Content-Type', $mime ? $mime : 'image/jpeg');
$this->response->body(file_get_contents($thumb));
} else {
throw new HTTP_Exception_500();
}
}
示例3: mime
/**
* Attempt to get the mime type from a file. This method is horribly
* unreliable, due to PHP being horribly unreliable when it comes to
* determining the mime type of a file.
*
* $mime = File::mime($file);
*
* @param string file name or path
* @return string mime type on success
* @return FALSE on failure
*/
public static function mime($filename)
{
// Get the complete path to the file
$filename = realpath($filename);
// Get the extension from the filename
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
if (preg_match('/^(?:jpe?g|png|[gt]if|bmp|swf)$/', $extension)) {
// Use getimagesize() to find the mime type on images
$file = getimagesize($filename);
if (isset($file['mime'])) {
return $file['mime'];
}
}
if (class_exists('finfo', FALSE)) {
if ($info = new finfo(defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME)) {
return $info->file($filename);
}
}
if (ini_get('mime_magic.magicfile') and function_exists('mime_content_type')) {
// The mime_content_type function is only useful with a magic file
return mime_content_type($filename);
}
if (!empty($extension)) {
return File::mime_by_ext($extension);
}
// Unable to find the mime-type
return FALSE;
}
示例4: action_copy
/**
* When Kohana::$environment is set to Kohana::DEVELOPMENT
* the asset will be served from this Controller.
* Otherwise the file will be served here once and then
* copied to DOCROOT and subsequently served from there.
*
* @return void
*/
public function action_copy()
{
$asset = $this->request->param('asset');
$last_modified = date(DATE_RFC1123, filemtime($asset));
if ($last_modified === $this->request->headers('if-modified-since'))
{
// Speed up the request by sending not modified
$this->response->status(304);
return;
}
if (Publicize::should_copy_to_docroot())
{
$uri = $this->request->param('uri');
Publicize::copy_to_docroot($asset, $uri);
}
$extension = pathinfo($asset, PATHINFO_EXTENSION);
// Bug in 3.2 prevents 'Content-Type' to work as expected -> 'content-type'
// see: https://github.com/kohana/core/commit/4605ccb6957a7b3a9854792328c937d1db003502
$this->response->headers(array(
'content-type' => File::mime_by_ext($extension),
'Content-Length' => (string) filesize($asset),
'Last-Modified' => $last_modified,
));
$this->response->body(file_get_contents($asset));
}
示例5: action_media
public function action_media()
{
if ($media = $this->request->param('media')) {
if (preg_match('/\\.swf$/iD', $media)) {
$file = preg_replace('/^(.+?)\\.swf$/iD', '$1', $media);
$this->request->headers['Content-Type'] = File::mime_by_ext('swf');
if ($file = Kohana::find_file('media', $file, 'swf')) {
$this->request->response = file_get_contents($file);
}
return;
}
$this->request->headers['Content-Type'] = File::mime_by_ext('js');
$file = preg_replace('/^(.+?)\\.js$/iD', '$1', $media);
if ($file = Kohana::find_file('media', $file, 'js')) {
$this->request->response = file_get_contents($file);
}
/*
foreach(explode('+', $media) as $file)
{
$file = preg_replace('/^(.+?)\.js$/iD', '$1', $file);
if($file = Kohana::find_file('media', $file, 'js'))
{
$this->request->response .= file_get_contents($file).PHP_EOL;
}
}
*/
}
}
示例6: before
public function before()
{
$directory = $this->request->directory();
$controller = $this->request->controller();
$action = $this->request->action();
$format = $this->_detect_response_format();
$mime = File::mime_by_ext($format) ?: 'text/html';
$this->response->view(['basedir' => $controller, 'directory' => $directory, 'action' => $action, 'format' => $format]);
$this->response->view->set(['basedir' => $controller, 'directory' => $directory, 'controller' => $controller, 'action' => $action, 'format' => $format, 'request' => $this->request, 'method' => $this->request->method(), 'secure' => $this->request->secure(), 'route' => $this->request->route(), 'route_name' => Route::name($this->request->route()), 'params' => $this->request->param(), 'query' => $this->request->query()]);
$this->format = $format;
if ($this->_layout === NULL) {
$layout = strtolower($controller);
if ($this->response->view->viewfile_exists($layout, 'layouts')) {
$this->_layout = $layout;
} else {
if ($this->response->view->viewfile_exists($this->_default_layout, 'layouts')) {
$this->_layout = $this->_default_layout;
}
}
}
$this->response->headers('Content-Type', $mime . '; charset=' . Kohana::$charset);
if ($this->_layout) {
View::layout($this->_layout);
}
$this->_before_action && $this->_call_method_for_action($this->_before_action);
}
示例7: get_mime_type
public function get_mime_type()
{
$extension = strtolower(pathinfo($this->_filename, PATHINFO_EXTENSION));
$mime = File::mime_by_ext($extension);
if (!$mime) {
$mime = 'application/octet-stream';
}
return $mime;
}
示例8: action_media
public function action_media($file)
{
$ext = pathinfo($file, PATHINFO_EXTENSION);
$file = substr($file, 0, -(strlen($ext) + 1));
if ($file = Kohana::find_file('media', $file, $ext)) {
$this->request->response = file_get_contents($file);
} else {
$this->request->status = 404;
}
$this->request->headers['Content-Type'] = File::mime_by_ext($ext);
}
示例9: action_token
/**
* This action issues access and refresh tokens and is called only
* by the 3rd party. All output should be JSON.
*
* You DO NOT need to extend/replace this action.
*/
public function action_token()
{
$this->response->headers('Content-Type', File::mime_by_ext('json'));
try {
// Attempt to issue a token
$this->response->body($this->_oauth->token());
} catch (OAuth2_Exception $e) {
// Something went wrong, lets give a formatted error
$this->response->status(400);
$this->response->body($e->getJsonError());
}
}
示例10: action_img
public function action_img($filename, $ext)
{
if (self::check(300) === FALSE) {
self::set(300);
}
$info = pathinfo($filename);
$file = Kohana::find_file('views/images', basename($info['basename']), $ext);
if (!$file) {
throw new Kohana_Exception("No such file or directory (:filename)", array('filename' => "{$filename}.{$ext}"));
}
$this->request->send_file($file, FALSE, array('inline' => 1, 'mime_type' => File::mime_by_ext($ext)));
}
示例11: get_response
/**
* Generate a Response for the 404 Exception.
*
* @return Response
*/
public function get_response()
{
$ext = pathinfo(Request::current()->url(), PATHINFO_EXTENSION);
$mimetype = FALSE;
if ($ext and !($mimetype = File::mime_by_ext($ext))) {
$mimetype = 'application/octet-stream';
}
if ($mimetype) {
return Response::factory()->headers('content-type', $mimetype)->status(404);
} else {
return parent::get_response();
}
}
示例12: action_media
public function action_media()
{
$file = $this->request->param('file');
$ext = pathinfo($file, PATHINFO_EXTENSION);
$file = substr($file, 0, -(strlen($ext) + 1));
if ($file = Kohana::find_file('media', $file, $ext)) {
$this->response->check_cache(sha1($this->request->uri()) . filemtime($file), $this->request);
$this->response->body(file_get_contents($file));
$this->response->headers('content-type', File::mime_by_ext($ext));
$this->response->headers('last-modified', date('r', filemtime($file)));
} else {
throw new HTTP_Exception_404('File not found.');
}
}
示例13: action_file
public function action_file()
{
$request = Request::instance();
$file = $request->param('file');
$ext = pathinfo($file, PATHINFO_EXTENSION);
$file = substr($file, 0, -(strlen($ext) + 1));
if ($file = Kohana::find_file('media', $file, $ext)) {
$request->response = file_get_contents($file);
} else {
Kohana::$log->add(Kohana::ERROR, 'Admin media controller error while loading file, ' . $file);
$request->status = 404;
}
$request->headers['Content-Type'] = File::mime_by_ext($ext);
}
示例14: get_response
public function get_response()
{
$ext = pathinfo(Request::current()->url(), PATHINFO_EXTENSION);
$mimetype = FALSE;
if ($ext and !($mimetype = File::mime_by_ext($ext))) {
$mimetype = 'application/octet-stream';
}
if ($mimetype and $mimetype !== 'text/html') {
return Response::factory()->headers('content-type', $mimetype)->status(404);
}
if (($page = Model_Page_Front::findByField('behavior_id', 'page_not_found')) !== FALSE) {
return Request::factory($page->url)->query('message', $this->message)->execute()->status(404);
}
throw new HTTP_Exception_404('Something went wrong');
}
示例15: action_img
public function action_img($filename = null)
{
if (!$filename) {
throw new Kohana_Exception("No such file or directory");
}
if (Kohana_Core::$environment != Kohana::DEVELOPMENT && self::check(300) === FALSE) {
self::set(300);
}
$info = pathinfo($filename);
$ext = $info['extension'];
$filename = $info['filename'];
$file = Kohana::find_file('views/images', basename($info['basename'], ".{$ext}"), $ext);
if (!$file) {
throw new Kohana_Exception("No such file or directory (:filename)", array('filename' => "{$filename}.{$ext}"));
}
$this->response->send_file($file, FALSE, array('inline' => 1, 'mime_type' => File::mime_by_ext($ext)));
}