绝对路径与相对路径 [英] Absolute vs. relative paths

查看:181
本文介绍了绝对路径与相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果使用绝对路径,则无法将整个目录移动到新位置. 如果使用相对路径,则无法将单个文件移动到新位置.

If I use absolute paths, I can't move the whole directory to a new location. If I use relative paths, I can't move individual files to new locations.

这里的解决方案是什么?您是否设置了一个包含根路径并从那里开始的配置文件?还是您有类似的规则:永不移动文件?

What's the solution here? Do you set up a config file that holds the root path and go from there? Or do you have a rule like: Never move files around?

我在某些项目中看到人们使用目录名( FILE ).我的意思是什么,为什么不直接将其删除,因为目录名仍然是相对的(取决于文件所在的位置)?

I've seen in some projects that people use dirname(FILE). What is the point of that, I mean, why not simply leave it out since the dirname is relative anyway (depending on where the file sits)?

推荐答案

您应该使用一个配置文件,该配置文件将包含在每个文件的第一行中,例如您的应用程序如下所示:

you should use a config file that will be included in each file first line, for example your app look like this

root/App/插件

root / App / Plugins

在您的根目录下:app-config.php

if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');

现在,假设您必须包含一个插件文件,所以

now, suppose you have to include a plugin file, so

在您的插件目录中:my-plugin.php

require_once '../../app-config.php';

现在此行下方的所有内容都可以使用ABSPATH

now everything below this line can use ABSPATH

您要加载图像的示例

<img src='".ABSPATH."Public/images/demo.png' alt=''/>

现在,如果您的应用旨在自动加载某些文件,例如

now, the thing is more simple if your app is designed to automatically load some files like

plugin-widget-1.php

,以便此文件或my-plugin.php文件加载的任何其他文件中的所有内容都可以使用ABSPATH,而不必每次app-config.php文件都包含.

so that everything inside this file or any other file loaded by the my-plugin.php file can use the ABSPATH without include each time the app-config.php file.

考虑到这一点,您可以将所需的所有速记方式放入app-config.php示例

with this in mind you can have all the short-hand you want into the app-config.php example

define('UPLOAD_PATH', ABSPATH. 'Public/uploads/');
define('IMAGES_PATH', ABSPATH. 'Public/images/');
define('HELPERS_PATH', ABSPATH. 'App/helpers/');
...

所以,既然已经定义好了,如果您需要移动一个文件,让我们说一个文件夹转发示例:

so, now that you have all defined, if you need to move a file, let's say one folder forward example:

root/App/Plugins/Utils

root / App / Plugins / Utils

只是喜欢require_once '../../../app-config.php';

很明显,我想您不是每次都在更改路径=)无论如何,如果要这样做,更改一个文件包含而不是数百个总是更简单.

obviously i suppose that you are not changing paths each time =) anyway if you need to do so is always more simple to change one file inclusion instead of hundreds.

希望这对您有意义=)

这篇关于绝对路径与相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆