php 如何在 Codeigniter 中加载图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13450608/
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
How to load Images in Codeigniter?
提问by Shakir Hossain
Hey i am totally new in Codeigniter. In the Controllers folder I created a file named caller.phpand I created a file home1.phpin \Views. In root directoryi created an image folder named \Imagesand also created a cssfolder named \css. In images Folder there are 6 picture. In css folder style.cssfile exist.
嘿,我是 Codeigniter 的新手。在 Controllers 文件夹中,我创建了一个名为caller.php的文件home1.php,并在\Views. 在root directory我创建了一个名为的图像文件夹\Images,还创建了一个css名为\css. 在图像文件夹中有 6 张图片。在 css 文件夹style.css文件中存在。
In caller.phpi write
在caller.php我写
<?
class caller extends CI_Controller
{
function index()
{
$this->load->view('home1');
// what i have to write here lo load images....
}
}
In home1.phpi write
在home1.php我写
<html>
<head>
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>css/style.css">
// what i have to write here to load images
</head>
<body>
<div id="outer">
<div id="container">
<div id="images">
<img src="new.jpg" width="960" height="400"/>
<img id="image1" src="1.jpg" />
<img id="image2" src="2.jpg" />
<img id="image3" src="3.jpg" />
<img id="image4" src="4.jpg" />
<img id="image5" src="5.jpg" />
</div>
<div id="slider">
<a href="#image1">1</a>
<a href="#image2">2</a>
<a href="#image3">3</a>
<a href="#image4">4</a>
<a href="#image5">5</a>
</div>
...................................................
....................................................
</div>
</div>
</body>
</html>
Now for the above code how i load the images .Please experts help me. if additional config's are needed please mention that.
现在对于上面的代码我如何加载图像。请专家帮助我。如果需要额外的配置,请提及。
回答by Dale
As you're already using the url helper I suggest wrapping your image src attributes with base_url, such as:
由于您已经在使用 url 助手,我建议您使用 base_url 包装您的图像 src 属性,例如:
<img src="<?php echo base_url('images/1.jpg'); ?>" />
And as mentioned in the other answer it's best (mandatory?) to capitalize the class name in your controller
正如另一个答案中提到的那样,最好(强制?)将控制器中的类名大写
class Caller extends CI_Controller { ...
回答by Kenzo
First of all, you need to capitalize your class name.
首先,您需要大写您的班级名称。
class Caller extends CI_Controller {
public function index()
{
// method code goes here
}
}
Then, you need to link to those images with absolute links or with the CI URL helper: "/images/1.jpg" and so on. How to use with the CI URL helper is detailed here: Helper
然后,您需要使用绝对链接或 CI URL helper 链接到这些图像:“/images/1.jpg”等。 此处详细介绍了如何与 CI URL 助手一起使用:Helper
EDIT
编辑
Load the URL helper with this in your constructor method:
在构造函数方法中使用此加载 URL 助手:
$this->load->helper('url');
You can create a URL like this:
您可以像这样创建一个 URL:
echo base_url("blog/post/123");
That will make:
这将使:
http://example.com/index.php/news/local/123
Or
或者
http://example.com/news/local/123
If you've taken out the index.php in your config file.
如果您已经删除了配置文件中的 index.php。
Here's a class with the constructor that calls the URL helper:
这是一个带有调用 URL 助手的构造函数的类:
class Caller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
// method code goes here
}
}
回答by sanjay kushwah
You can do the same for as you did to Load the CSS files.
Create a folder in Root Directory. Create a sub folder(if you have Multisite)
then point your base_url in config file to your base path.
您可以执行与加载 CSS 文件相同的操作。
在根目录中创建一个文件夹。创建一个子文件夹(如果您有 Multisite),
然后将配置文件中的 base_url 指向您的基本路径。
and then do the same<img id="image1" src="<?PHP echo base_url(); ?>images/[sub-folder]/1.jpg" />
然后做同样的事情<img id="image1" src="<?PHP echo base_url(); ?>images/[sub-folder]/1.jpg" />
回答by Daxen
Only simple way
只有简单的方法
<img id="image1" src="<?php echo base_url('1.jpg'); ?>" />
That sit!!!
就是这样!!!
回答by Manjitha teshara
Make folder in root directory and code easily in views html file.
在根目录中创建文件夹并在视图 html 文件中轻松编写代码。
if directory name is directoryName and image name imageName.jpg
如果目录名称是 directoryName 和图像名称 imageName.jpg
you can call like this.
你可以这样打电话。
<img src="directoryName/imageName.jpg">

