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

CodeigniterWeb ApplicationsFrameworksCodeigniter 2

Codeigniter Problem Overview


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.

Codeigniter Solutions


Solution 1 - 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:

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.

Solution 2 - Codeigniter

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

Solution 3 - Codeigniter

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

Solution 4 - Codeigniter

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.

Solution 5 - Codeigniter

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.

Solution 6 - Codeigniter

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.

Solution 7 - Codeigniter

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>

Solution 8 - Codeigniter

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.

Solution 9 - Codeigniter

I use the following folder structure:

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

Solution 10 - Codeigniter

(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]

Solution 11 - Codeigniter

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';

Solution 12 - Codeigniter

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

Solution 13 - Codeigniter

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.

Solution 14 - Codeigniter

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" />

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontriqView Question on Stackoverflow
Solution 1 - CodeigniterEddieView Answer on Stackoverflow
Solution 2 - CodeigniterJuan JoView Answer on Stackoverflow
Solution 3 - CodeigniterIjas AmeenudeenView Answer on Stackoverflow
Solution 4 - Codeignitercitizen connView Answer on Stackoverflow
Solution 5 - CodeigniterKrekerView Answer on Stackoverflow
Solution 6 - CodeigniterCheesus ToastView Answer on Stackoverflow
Solution 7 - CodeigniterSandeep MehraView Answer on Stackoverflow
Solution 8 - CodeignitercoolestView Answer on Stackoverflow
Solution 9 - CodeigniterScorpionView Answer on Stackoverflow
Solution 10 - CodeigniterAnthony ScaifeView Answer on Stackoverflow
Solution 11 - CodeigniterRabab ShView Answer on Stackoverflow
Solution 12 - CodeigniterPiyush DhanotiyaView Answer on Stackoverflow
Solution 13 - Codeigniterron_gView Answer on Stackoverflow
Solution 14 - CodeigniterStephen CView Answer on Stackoverflow