如何显示图像与Codeigniter php框架? [英] How can I display images with the Codeigniter php framework?

查看:74
本文介绍了如何显示图像与Codeigniter php框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Codeigniter php框架显示图片?

How can I display images with the Codeigniter php framework?

我要从图片提取图片

I want to fetch an image from the images folder inside views.

我使用这个代码在 views php档案:

I used this code in one of the views php files:

<img border="0" src="images/hed_05.jpg" width="61" height="133">

但它不工作。

推荐答案

图片,css,javascript,pdfs,xml ...允许直接访问 的任何东西都不应该居住在你的应用程序目录中。你可以做,但你真的不应该。在目录的根目录下为这些文件创建一个新文件夹,它们不应该混合到您的应用程序中,例如:在 views 文件夹中。

Images, css, javascript, pdfs, xml... anything that is allowed to be accessed directly should not be living in your application directory. You can do it, but you really shouldn't. Create a new folder at the root of your directory for these files, they should not be mixed into your application, for example: in your views folder.


  1. 很可能是你正在使用.htaccess文件,它只允许通过http访问某些目录。这是非常好的安全原因,你想停止任何尝试直接访问您的控制器和模型。这也是为什么我们检查 BASEPATH 是否定义在大多数文件的顶部, exit('无直接脚本访问。

  2. 要获得这些资源的正确路径(js / css / images),您不能使用相对路径,因为我们不使用正常目录结构。 url / users / login 未从目录 / users / login 加载文件,存在。这些只是url段,引导程序使用它来知道要使用哪个类,方法和参数。

  1. Chances are, you're using an .htaccess file, which is only allowing certain directories to be accessed via http. This is very good for security reasons, you want to stop any attempt to access your controllers and models directly. This is also why we check if BASEPATH is defined at the top of most files, and exit('No direct script access.') if not.
  2. To get the correct path to these resources (js/css/images), you can't use relative paths, because we aren't using a normal directory structure. The url /users/login is not loading files from the directory /users/login, it probably doesn't even exist. These are just uri segments that the bootstrap uses to know which class, method, and params to use.

要获取正确的路径,请使用正斜杠(假设您的应用和资产位于根目录中,而不是子目录)像这样:

To get the correct path, either use a forward slash (assuming your app and assets are in the root directory, not a subdirectory) like this:

<img src="/images/myimage.jpg" />

或者您最好下注,使用绝对网址:

Or your best bet, use an absolute url:

// References your $config['base_url']
<img src="<?php echo site_url('images/myimage.jpg'); ?>" />

等效于:

<img src="http://mydomain.com/images/myimage.jpg" />

CI中内置了帮助函数,您可以选择使用,但这真的是你需要的知道。

There are helpers built into CI that you can optionally use as well, but this is really all you need to know.

这篇关于如何显示图像与Codeigniter php框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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