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

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

问题描述

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

How can I display images with the Codeigniter php framework?

我想从 views 内的 images 文件夹中获取图像.

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('No direct script access.').
  2. 要获取这些资源 (js/css/images) 的正确路径,您不能使用相对路径,因为我们没有使用正常的目录结构.url /users/login 没有从目录 /users/login 加载文件,它甚至可能不存在.这些只是引导程序用来了解要使用的类、方法和参数的 uri 段.
  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天全站免登陆