Where do I put image files, css, js, etc. in Codeigniter? Where do I put image files, css, js, etc. in Codeigniter? codeigniter codeigniter

Where do I put image files, css, js, etc. in Codeigniter?

I have a setup like this:

  • application
  • system
  • assets
    • js
    • imgs
    • css

I then have a helper function that simply returns the full path to this depending on my setup, something similar to:


function asset_url(){   return base_url().'assets/';}

I will usually keep common routines similar to this in the same file and autoload it with codeigniter's autoload configuration.

Note: autoload URL helper for base_url() access.


$autoload['helper'] = array('url','utility');

You will then have access to asset_url() throughout your code.

No, inside the views folder is not good.

Look:You must have 3 basic folders on your project:

system // This is CI framework there are not much reasons to touch this files

application //this is where your logic goes, the files that makes the application,

public // this must be your documentroot

For security reasons its better to keep your framework and the application outside your documentroot,(public_html, htdocs, public, www... etc)

Inside your public folder, you should put your public info, what the browsers can see, its common to find the folders: images, js, css; so your structure will be:

|- system/|- application/|---- models/|---- views/|---- controllers/|- public/|---- images/|---- js/|---- css/|---- index.php|---- .htaccess

This is how I handle the public assets. Here I use Phalcon PHP's Bootstrap method.

Folder Structure

|-.htaccess*|-application/|-public/  |-.htaccess**  |-index.php***  |-css/  |-js/  |-img/  |-font/  |-upload/|-system

Files to edit

  1. .htaccess*

    All requests to the project will be rewritten to the public/ directory making it the document root. This step ensures that the internal project folders remain hidden from public viewing and thus eliminates security threats of this kind. - Phalconphp Doc

    <IfModule mod_rewrite.c>    RewriteEngine on    RewriteRule  ^$ public/    [L]    RewriteRule  (.*) public/$1 [L]</IfModule>
  2. .htaccess**

    Rules will check if the requested file exists and, if it does, it doesn’t have to be rewritten by the web server module. - Phalconphp Doc

    <IfModule mod_rewrite.c>    RewriteEngine On    RewriteCond %{REQUEST_FILENAME} !-d    RewriteCond %{REQUEST_FILENAME} !-f    RewriteRule ^(.*)$ index.php/$1 [QSA,L]</IfModule>
  3. index.php***

    Modify the application path and the system path as,

    $system_path = '../system';$application_folder = '../application';

Now you use the public folder as base_url()."public/[YOUR ASSET FOLDER]"

Hope this helps :)