3

I have a problem with Codeigniter URL. I have a controller "welcome.php" :

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $data['tiung'] = 'index';
        $this->load->view('welcome_message',$data);
    }

    public function dor($bus)
    {
        $data['tiung'] = $bus;
        $this->load->view('welcome_message',$data);
    }
}

and a view "welcome_message.php" :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Welcome to CodeIgniter</title>
</head>
<body>
    sesuatu <?php echo $tiung?>
    <img src="fragor.jpg" width="720" height="246" alt=""/>
    ladalah
</body>
</html>

If I want to access the controller function 'dor' with a parameter I used this :

localhost/hostname/index.php/welcome/dor/something

and it works, but the problem is the image isn't loaded. I tried to put the image file in the webroot folder, 'application' folder, and even in the 'views' folder. But the image still can't load. Where should I put the image file?

1
  • assets folder or css folder, then use the site_url funtion like site_url('assets/fragor.jpg'); Commented Aug 24, 2012 at 13:17

3 Answers 3

5

The best practice for this is to create an /images/ directory in your webroot (the folder with index.php) and use the base_url() function to get the link to the image, I.E.

<img src="<?php echo base_url('images/fragor.jpg'); ?>" width="720" height="246" alt=""/>

Don't forget to load the url helper either with the autoloader or manually before using site_url() or base_url().

Also if you're using CodeIgniter's rewrite rules to remove the index.php from the URL (which it doesn't look like you're doing), don't forget to exclude the /images/ directory from the rewrite.

Sign up to request clarification or add additional context in comments.

1 Comment

the problem is i have designed the web using dreamweaver. changing the site using "<?php echo base_url()?>" means i have to change a lot of url links in my pages. is there any other solution without changing the url links?
1

In CodeIgniter there is a specific way to indicate an image...like

echo img('images/gautam.gif');

put this in your view file and you need to create "images" folder at your root

Comments

0

Though you have to add CI helper 'html' in autoload config, but this goes easy after.

<?php
$image_properties = array(
    'src'   => 'images/picture.jpg',
    'alt'   => 'Me, demonstrating how to eat 4 slices of pizza at one time',
    'class' => 'post_images',
    'width' => '200',
    'height'=> '200',
    'title' => 'That was quite a night',
    'rel'   => 'lightbox'
);
img($image_properties);
?>
/* <img src="site.com/index.php/images/picture.jpg" alt="Me, demonstrating how to eat 4 slices of pizza at one time" class="post_images" width="200" height="200" title="That was quite a nigh`enter code here`t" rel="lightbox" />*/

OR JUST simple:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.