有没有办法让 Codeigniter 控制器返回图像? [英] Is there a way to have a Codeigniter controller return an image?

查看:23
本文介绍了有没有办法让 Codeigniter 控制器返回图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以让控制器返回一个图像(无论是 JPG、PNG 等),而不是返回字符串或视图.例如,不是以 $this->load->view('folder/special_view.php) 结尾,我想做类似 $this->load->image('images/gorilla.png') 的事情,并拥有它,如果我的用户要转到该控制器,他们会看到一个图像,就好像他们转到了普通的 .png 或 jpeg.我可以设置标题以便它期望不同的 MIME 吗?这样的示例代码会很棒.

I was wondering if there was a way for a controller to, instead of returning a string, or a view, return an image (be it JPG, PNG etc). For example, instead of ending with a $this->load->view('folder/special_view.php), I'd like to do something like $this->load->image('images/gorilla.png'), and have it so if my user were to go to that controller they would see an image as if they'd gone to a normal .png or jpeg. Can I set the headers so it expects a different MIME? Example code of this would be fantastic.

我需要花很长时间来解释为什么我需要这个,但它涉及将预制的 CMS 带入 codeigniter,并让它需要某些特定的东西才能成为现实.非常感谢!

It would take forever for me to explain why I need this, but it involves bringing a premade CMS into codeigniter, and having it need certian things to be true. Thank you so much!

推荐答案

当然可以,用这个代替 $this->load->view()

sure you can, use this instead of $this->load->view()

$filename="/path/to/file.jpg"; //<-- specify the image  file
if(file_exists($filename)){ 
  $mime = mime_content_type($filename); //<-- detect file type
  header('Content-Length: '.filesize($filename)); //<-- sends filesize header
  header("Content-Type: $mime"); //<-- send mime-type header
  header('Content-Disposition: inline; filename="'.$filename.'";'); //<-- sends filename header
  readfile($filename); //<--reads and outputs the file onto the output buffer
  exit(); // or die()
}

这篇关于有没有办法让 Codeigniter 控制器返回图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆