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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5656894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:07:47  来源:igfitidea点击:

How can I display images with the Codeigniter php framework?

phphtmlcodeigniter

提问by Buffon

How can I display images with the Codeigniter php framework?

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

I want to fetch an image from the imagesfolder inside views.

我想从images里面的文件夹中获取图像views

I used this code in one of the viewsphp files:

我在其中一个viewsphp 文件中使用了此代码:

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

But it is not working.

但它不起作用。

回答by Wesley Murch

Images, css, javascript, pdfs, xml... anything that is allowed to be accessed directlyshould not be living in your application directory. You cando 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 viewsfolder.

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

  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 BASEPATHis 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/loginis 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.
  1. 很有可能,您正在使用 .htaccess 文件,该文件只允许通过 http 访问某些目录。出于安全原因,这非常好,您希望停止任何直接访问控制器和模型的尝试。这也是为什么我们检查是否BASEPATH在大多数文件的顶部定义,exit('No direct script access.')如果没有。
  2. 要获得这些资源的正确路径 (js/css/images),您不能使用相对路径,因为我们没有使用正常的目录结构。url/users/login没有从目录加载文件/users/login,它甚至可能不存在。这些只是引导程序用来知道要使用哪个类、方法和参数的 uri 段。

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'); ?>" />

Equivalent to:

相当于:

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

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

您也可以选择使用 CI 中内置的帮助程序,但这确实是您需要知道的全部内容。

回答by Dry

Please see the CI User Guide/HTML Helperimg() tag:

请参阅CI 用户指南/HTML Helperimg() 标签:

echo img('images/picture.jpg');
// gives <img src="http://site.com/images/picture.jpg" />

回答by Ross

You can also just set your base hrefin the <head>:

您也可以将您base href的设置在<head>

<base href="<?php echo site_url(); ?>" />

<base href="<?php echo site_url(); ?>" />

Now any image, css file and other asset will be relative to your base URL.

现在任何图像、css 文件和其他资产都将与您的基本 URL 相关。

<base href="http://site.com/" />

<base href="http://site.com/" />

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

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

is in effect

有效

<img src="http://site.com/images/foo.jpg" />

<img src="http://site.com/images/foo.jpg" />

Your images should be outside of the application folder (say in /images) however.

但是,您的图像应该在应用程序文件夹之外(例如在 中/images)。

回答by Galen

Perhaps the path in the srcattribute is incorrect. Maybe try

也许src属性中的路径不正确。也许试试

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