1

I need to define a few directory paths for an application.

The folder setting

The config.php is in the config folder which is placed outside the root directory (best practice, read somewhere). The application is placed in the root directory (myCms, in this case). Right now I am on a local server (xampp).

enter image description here

The config.php has some configuration and few credentials as required by the program to function.

As said, I need to define a few directories (images, template, system so on and so forth) in the config.php file. I previously defined them as

define('DIRIMAGES','C:/xampp/htdocs/myCms/images/', false);

I was planning to use the realpath() function to get the job done and tried this

define('DIRIMAGES', str_replace('\\', '/',realpath(dirname(__FILE__))).'/images/', false);

Later I included the config.php in the index.php (index.php in myCms folder) and echoed to check the path of the directory and it shows

C:/xampp/htdocs/config/system/

Where as I was looking for:

C:/xampp/htdocs/myCms/system/

Can you suggest or help?

2
  • Have you tried just __DIR__? Commented Aug 26, 2015 at 18:35
  • This is because both __DIR__ and __FILE__ are relative to the file they are written in, not the file that includes them. Commented Aug 26, 2015 at 19:00

1 Answer 1

2

__FILE__ refers to the file currently being processed. If you move your config file around, all your generated paths are going to change too.

Instead of dirname(__FILE__) you can just use __DIR__, assuming you're in PHP 5.3+

To generate a path to the images subdirectory of myCms, you would do it like this (if your config file is in the config folder!)

define('DIRIMAGES', realpath(__DIR__ . '/../myCms/images/'));

So you start off in config, then go up a level to your root folder (which contains both your config and myCms folders), then into myCms, and finally into images.

Sign up to request clarification or add additional context in comments.

3 Comments

i too thought on the same lines after Rasclatt pointed out. Thank you. I'll try what you suggested.
I would recommend storing realpath(__DIR__ . '/../myCms') in a separate definition so that it can easily be reused for your other paths.
sure ... and will get back with the results after trying

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.