How To Get CodeIgniter 4 Public Directory Path Example

Reading Time: 5 minutes
14,513 Views

Understanding how to retrieve the path to the public directory in CodeIgniter 4 is a basic component of handling files and assets within your online application.

The public directory, sometimes known as the “public” directory, includes files that users can view directly through their web browsers. CSS, JavaScript, pictures, and other materials designed to improve the user experience may be included.

When you install CodeIgniter 4, initially application runs from /public folder. By default, It is the path of front controller. There are several pre defined CodeIgniter 4 global constant variables. Those variables returns the path of folders like /app, /writable, etc.

This tutorial will walk you through the process of obtaining the path to the public directory in CodeIgniter 4. We’ll look at how to use the built-in utility function public_path() to find the location of the public directory.

Read More: How To Connect CodeIgniter 4 with Multiple Databases?

Let’s get started.

CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.

Environment (.env) Setup

When we install CodeIgniter 4, we will have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Either we can do via renaming file as simple as that. Also we can do by terminal command.

Open project in terminal

cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# CI_ENVIRONMENT = production

 // Do it to 
 
CI_ENVIRONMENT = development

Now application is in development mode.

What is a Global Constant Variable in Codeigniter 4?

A global constant variable in CodeIgniter 4 is a value that remains persistent and accessible throughout the application’s runtime.

Global constants are often defined at the application’s top level and retain their value regardless of where they are accessed. They are handy for storing values that must be consistent across multiple portions of your programme.

Global constants in CodeIgniter 4 are defined using the define() function, which is a built-in PHP function for creating constants.

Examples: APPPATH, ROOTPATH, etc.

Read More: How To Integrate ChatGPT API in CodeIgniter 4 Tutorial

Codeigniter 4 Default Global Constants

There are several global constants available in application to use.

How to use & access the path value?

APPPATH (Application folder path)

Use

echo APPPATH; // returns the complete path upto /app folder.

Example

/var/www/html/codeigniter-4/app/

FCPATH (Front controller path)

Use

echo FCPATH; // returns the complete path upto /public folder.

Example

/var/www/html/codeigniter-4/public/

ROOTPATH (Application root)

Use

echo ROOTPATH; // returns the complete path upto / folder.

Example

/var/www/html/codeigniter-4/

WRITEPATH (Writable folder path)

Use

echo WRITEPATH; // returns the complete path upto /writable folder.

Example

/var/www/html/codeigniter-4/writable/

SYSTEMPATH (System folder path)

Use

echo SYSTEMPATH; // returns the complete path upto /system folder.

Example

/var/www/html/codeigniter-4/vendor/codeigniter4/framework/system/

Read More: Create Automated Logs Every 2 hours in CodeIgniter 4 Tutorial

How to Create User Defined Constant?

Optionally, you can also create a global constant for the path upto /public folder.

Open Constants.php from /app/Config folder.

Add this line of code into it.

defined('PUBLIC_PATH') || define('PUBLIC_PATH', ROOTPATH . 'public');

ROOTPATH returns the path of application root. Only you need to concat with public folder.

Inside any file you can call as,

echo PUBLIC_PATH; // returns path of /public folder.

That’s it.

We hope this article helped you to learn about How To Get CodeIgniter 4 Public Directory Path Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.