简体   繁体   English

在PHP中使用realpath()函数定义目录路径

[英]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). config.php位于config文件夹中,该文件夹位于根目录之外(最佳实践,请在某处读取)。 The application is placed in the root directory (myCms, in this case). 该应用程序位于根目录(在本例中为myCms)中。 Right now I am on a local server (xampp). 现在,我在本地服务器(xampp)上。

在此处输入图片说明

The config.php has some configuration and few credentials as required by the program to function. config.php具有程序需要的一些配置和少量凭据才能正常运行。

As said, I need to define a few directories (images, template, system so on and so forth) in the config.php file. 如前所述,我需要在config.php文件中定义一些目录(图像,模板,系统等)。 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 我打算使用realpath()函数来完成工作并尝试

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 后来我将config.php包含在index.php中(myCms文件夹中的index.php)并回显以检查目录的路径,并显示

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. __FILE__是指当前正在处理的文件。 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+ 如果您使用的是PHP 5.3+,则可以使用__DIR__代替dirname(__FILE__)

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!) 要生成指向myCms的images子目录的路径,您可以按以下方式进行操作(如果您的配置文件位于config文件夹中!)

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. 因此,您从config开始,然后上升到根文件夹(其中同时包含config和myCms文件夹),然后进入myCms,最后进入映像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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