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

Where is it acceptable to put css folders and image file folders? I was thinking inside the view folder? However the controller always reroutes the path to the base url so I have to specify the path in the .html file to where it sits, which is redundant.

The answer is


I use the following folder structure:

application 
system 
static
    admin
        js
        css
        images
    public
        js
        css
        images
    uploads
        original
        thumbs

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:

application/helpers/utility_helper.php:

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.

application/config/autoload.php:

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

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


I usually put all my files like that into an "assets" folder in the application root, and then I make sure to use an Asset_Helper to point to those files for me. This is what CodeIgniter suggests.


Hi our sturucture is like Application, system, user_guide

create a folder name assets just near all the folders and then inside this assets folder create css and javascript and images folder put all your css js insiide the folders

now go to header.php and call the css just like this.

<link rel="stylesheet" href="<?php echo base_url();?>assets/css/touchTouch.css">
  <link rel="stylesheet" href="<?php echo base_url();?>assets/css/style.css">
  <link rel="stylesheet" href="<?php echo base_url();?>assets/css/camera.css"> 

No one says that you need to modify the .htacces and add the directory in the rewrite condition. I've created a directory "public" in the root directory alongside with "system", "application", "index.php". and edited the .htaccess file like this:

RewriteCond $1 !^(index\.php|public|robots\.txt)

Now you have to just call <?php echo base_url()."/public/yourdirectory/yuorfile";?> You can add subdirectory inside "public" dir as you like.


Regardless of where you put the CSS file you can simply use the CodeIgniter "html" helper called link_tag(). You would apply this helper something like this:

echo link_tag('folder/subfolder/stylesheet.css');

It is up to you to locate the CSS file in the most appropriate place (in your opinion). You would have to either autoload the "html" helper or separately load it in to use it.

People keep suggesting the usage of base_url() to achieve this but it is actually not really the "CodeIgniter" way to do it (I think this question has a few duplicates). That will work but one of the reasons for using a framework is to use the pre-made functions within it.

There are many helpers in codeigniter to do this kind of thing.


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

add one folder any name e.g public and add .htaccess file and write allow from all it means, in this folder your all files and all folder will not give error Access forbidden! use it like this

<link href="<?php echo base_url(); ?>application/public/css/style.css" rel="stylesheet" type="text/css"  />
<script type="text/javascript" src="<?php echo base_url(); ?>application/public/js/javascript.js"></script>

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 :)


The link_tag() helper is clearly the way to do this. Put your css where it usually belongs, in site_root/css/ and then use the helper:

From The CodeIgniter docs...

echo link_tag('css/mystyles.css');

Output

<link href="http://example.com/css/mystyles.css" rel="stylesheet" type="text/css" />

(I am new to Codeigniter, so I don't know if this is the best advice)

I keep publicly available files in my public folder. It is logical for them to be there, and I don't want to use Codeigniter for something I don't need to use it for.

The directory tree looks likes this:

  • /application/
  • /public/
  • /public/css/
  • /public/img/
  • /public/js/
  • /system/

The only files I keep in /public/ are Codeigniter's index.php, and my .htaccess file:

RewriteEngine On
RewriteBase /

RewriteRule ^css/ - [L]
RewriteRule ^img/ - [L]
RewriteRule ^js/ - [L]

RewriteRule ^index.php(.*)$ - [L]
RewriteRule ^(.*)$ /index.php?/$1 [L]

you can put the css folder inside the assest folder(you name it any name) in the directory of your project as:

- ci_app
- application 
- views      
- assets 
  - css 
    - style.css 

... when you want to load that file in a page, you can use base_url()function as this way: <head> <link rel='stylesheet' href='<?php echo base_url();?>assets/css/style.css'> </head> and you are sure to add base_url of your project in the config.php file as this:

$config['base_url'] = 'http://localhost/ci_app';

I'm using the latest version of CodeIgniter 3.1.0. The folder structure of it is:

  • system
  • application
  • user_guide
  • assets
    • images
    • js
    • css

That's where you should put the images, css and js files inside the assets folder.


I just wanted to add that the problem may be even simpler -

I've been scratching my head for hours with this problem - I have read all the solutions, nothing worked. Then I managed to check the actual file name.

I had "image.jpg.jpg" rather than "image.jpg".

If you use $ ls public/..path to image assets../ you can quickly check the file names.

Sounds stupid but I never thought to look at something so simple as file name given the all the technical advice here.


Examples related to codeigniter

DataTables: Cannot read property 'length' of undefined How to set time zone in codeigniter? php codeigniter count rows CodeIgniter: 404 Page Not Found on Live Server Only variable references should be returned by reference - Codeigniter How to pass a parameter like title, summary and image in a Facebook sharer URL How to JOIN three tables in Codeigniter how to get the base url in javascript How to get id from URL in codeigniter? How to disable PHP Error reporting in CodeIgniter?

Examples related to web-applications

Spring - No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call How do I choose the URL for my Spring Boot webapp? Difference between MEAN.js and MEAN.io External VS2013 build error "error MSB4019: The imported project <path> was not found" How to unpackage and repackage a WAR file IntelliJ, can't start simple web application: Unable to ping server at localhost:1099 Using form input to access camera and immediately upload photos using web app Pass user defined environment variable to tomcat ASP.NET: HTTP Error 500.19 – Internal Server Error 0x8007000d Best practices when running Node.js with port 80 (Ubuntu / Linode)

Examples related to frameworks

Undefined Symbols error when integrating Apptentive iOS SDK via Cocoapods Vue.js—Difference between v-model and v-bind OS X Framework Library not loaded: 'Image not found' How to get root directory in yii2 Laravel blank white screen Could not load file or assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies How to filter (key, value) with ng-repeat in AngularJs? Microsoft .NET 3.5 Full download Using CSS in Laravel views? Difference between framework vs Library vs IDE vs API vs SDK vs Toolkits?

Examples related to codeigniter-2

Only variable references should be returned by reference - Codeigniter Codeigniter: does $this->db->last_query(); execute a query? Using Mysql WHERE IN clause in codeigniter CodeIgniter PHP Model Access "Unable to locate the model you have specified" Where do I put image files, css, js, etc. in Codeigniter? CodeIgniter: How to get Controller, Action, URL information