简体   繁体   中英

PHP CodeIgniter (how to make public folder)

I'm a new in CodeIgniter.

I made public folder, where I want to put my css/js/images folders.

My setup is like that:

application
   cache
   config
   controllers
   public
       css
       js
       images
  ....
  ....
  ....

My .htaccess is:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /moviesmvc/

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]

    </IfModule>

    <IfModule !mod_rewrite.c>
        # If we don't have mod_rewrite installed, all 404's
        # can be sent to index.php, and everything works as normal.
        # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule> 

how can I make this public folder public?

/////////////////////////

You really don't want to put your public folder inside your application folder. Your directory structure should be like this

 -- application
 -- system
 -- public
    -- css
    -- js
    -- images
    -- index.php
    -- .htaccess

This is the best practice to follow and you will save yourself a lot of time and potential problems in the future

If it's a Linux server then you need to change the folder permissions to 755 or 777. Have a Google around for this and you will get a lot more detail.

I would recommend not using CI at the minute as it isn't being developed on any more until they find a new owner. Instead, I would recommend Laravel as it's such an amazing framework, but you should also try out CakePHP, Symfony, Yii or something else which is still being developed.

Check Your Public Folder Permission as well as use base_url() function to get the public url..

for example

echo base_url('public/images'); to get image public path

I would not advise to implement this type of design and here is why:

  • Web servers are designed to not allow public access below the document root. This aides in both ease-of-setup and security.

  • If a major security change is made within the root then it has to be duplicated to your public folder.

  • You are adding unnecessary processing to EVERY HTTP/HTTPS call to your web server because now the rules have to be processed for every request. If you could build this into your httpd.conf file instead then the overhead is nearly non-existent.

TL;DR;

No need to re-invent the wheel by adding a lump around the perimeter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM