How to load Images in Codeigniter?

2019-07-07 06:22发布

问题:

Hey i am totally new in Codeigniter. In the Controllers folder I created a file named caller.php and I created a file home1.php in \Views. In root directory i created an image folder named \Images and also created a css folder named \css. In images Folder there are 6 picture. In css folder style.css file exist.

In caller.php i write

<?
class caller extends CI_Controller
{
    function index()
    {
        $this->load->view('home1');

        // what i have to write here lo load images....

    }
}

In home1.php i write

<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.

回答1:

As you're already using the url helper I suggest wrapping your image src attributes with base_url, such as:

<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 { ...


回答2:

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

EDIT

Load the URL helper with this in your constructor method:

    $this->load->helper('url');

You can create a URL like this:

    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.

Here's a class with the constructor that calls the URL helper:

    class Caller extends CI_Controller { 

        public function __construct() 
        { 
            parent::__construct();
            $this->load->helper('url'); 
        } 

        public function index() 
        { 
            // method code goes here 
        } 
    } 


回答3:

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.

and then do the same
<img id="image1" src="<?PHP echo base_url(); ?>images/[sub-folder]/1.jpg" />



回答4:

Only simple way

<img id="image1" src="<?php echo base_url('1.jpg'); ?>" />

That sit!!!



回答5:

Make folder in root directory and code easily in views html file.

if directory name is directoryName and image name imageName.jpg

you can call like this.

<img src="directoryName/imageName.jpg">