简体   繁体   中英

defining directory path using realpath() function in php

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

在此处输入图片说明

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?

__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.

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