Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display images with the Codeigniter php framework? [closed]

How can I display images with the Codeigniter php framework?

I want to fetch an image from the images folder inside views.

I used this code in one of the views php files:

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

But it is not working.

like image 898
Buffon Avatar asked Apr 13 '11 23:04

Buffon


People also ask

Is CodeIgniter PHP framework?

CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications. Why CodeIgniter?

Is CodeIgniter still good?

CodeIgniter is still exist but the name of this framework has become synonymous with a low-quality solution that is why we advise you Laravel as a better alternative. Both Laravel and CodeIgniter are open-source PHP framework.


1 Answers

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

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.

like image 187
Wesley Murch Avatar answered Oct 27 '22 06:10

Wesley Murch