lets say i have a directory structure like this
假设我有一个像这样的目录结构
____
| Root
|_____
Folder1
|_____
Folder2
|_____
Subfolder2.1
Subfolder2.2
|______
Subfolder2.2.1
if i wanted to move upwards from Subfolder2.1
to the Root
folder i would have to do ../../
and if i have to move from Subfolder2.2.1
to Root
i would have to do ../../../
is there a way in php or in regular expression to write an expression that goes back up the directory until it finds a specific file name (root)
如果我想从Subfolder2.1向上移动到Root文件夹我将不得不做../../如果我必须从Subfolder2.2.1移动到Root我将不得不做../../ .. /有没有办法在PHP或正则表达式中编写一个表达式,该表达式返回目录,直到找到特定的文件名(root)
currently in symfony framework i'm doing something like $defaultTemplate = '::../../../../../app/Resources/views/'.$input->getOption('mytwigdest')
to create a template inside a directroy
目前在symfony框架中,我正在做类似$ defaultTemplate =':: ../../../../../ app / Resources / views /'.$ input-> getOption('mytwigdest')to to在directroy中创建一个模板
ofcourse this only works for one level of folders not its subfolders or its parent folder, what expression do i need to put in instead of the ../
so that it goes up the directories until it finds a specific directory name
当然,这只适用于一个级别的文件夹而不是它的子文件夹或其父文件夹,我需要放入什么表达式而不是../,以便它上升到目录,直到找到特定的目录名称
__ DIR __ and symfony's $this->container->getParameter('kernel.root_dir')
would not work as it would give me the current file's path which is entirely different from where i'm creating a template using $defaultTemplate
__ DIR __和symfony的$ this-> container-> getParameter('kernel.root_dir')无效,因为它会给我当前文件的路径,这与我使用$ defaultTemplate创建模板的地方完全不同
2 个解决方案
#1
1
Although I'm not sure why you need to go with such a way to solve your problem, this will calculate number of parent directories ../
to a custom directory. You only want to change the name. Here it is Root
:
虽然我不确定为什么你需要采用这种方式来解决你的问题,但这将计算父目录的数量../到自定义目录。您只想更改名称。这是Root:
str_repeat("../", count(explode(DIRECTORY_SEPARATOR , __FILE__)) - (1 + array_flip(explode(DIRECTORY_SEPARATOR , __FILE__))['Root']));
Put all things together:
把所有东西放在一起:
$defaultTemplate = '::'.str_repeat("../", count(explode(DIRECTORY_SEPARATOR , __FILE__)) - (1 + array_flip(explode(DIRECTORY_SEPARATOR , __FILE__))['Root'])).'app/Resources/views/'.$input->getOption('mytwigdest');
#2
0
include($_SERVER['DOCUMENT_ROOT'].'/index.php');
Will always find the direct to root folder. Note you need /
after $_SERVER.
总会找到直接到根文件夹。注意你需要/在$ _SERVER之后。
#1
1
Although I'm not sure why you need to go with such a way to solve your problem, this will calculate number of parent directories ../
to a custom directory. You only want to change the name. Here it is Root
:
虽然我不确定为什么你需要采用这种方式来解决你的问题,但这将计算父目录的数量../到自定义目录。您只想更改名称。这是Root:
str_repeat("../", count(explode(DIRECTORY_SEPARATOR , __FILE__)) - (1 + array_flip(explode(DIRECTORY_SEPARATOR , __FILE__))['Root']));
Put all things together:
把所有东西放在一起:
$defaultTemplate = '::'.str_repeat("../", count(explode(DIRECTORY_SEPARATOR , __FILE__)) - (1 + array_flip(explode(DIRECTORY_SEPARATOR , __FILE__))['Root'])).'app/Resources/views/'.$input->getOption('mytwigdest');
#2
0
include($_SERVER['DOCUMENT_ROOT'].'/index.php');
Will always find the direct to root folder. Note you need /
after $_SERVER.
总会找到直接到根文件夹。注意你需要/在$ _SERVER之后。