简体   繁体   English

设置自定义“根”文件夹以更轻松地包含/需要更深层次的php文件

[英]Setting custom “root” folder to include/require php files within deeper levels easier

I was trying to come up with a solution to a problem that I encountered while programming in PHP. 我试图为我在PHP编程时遇到的问题提供解决方案。 I am trying to set a custom "root" folder. 我试图设置一个自定义的“根”文件夹。 I am using MAMP in my computer, so I would like to set as my "root" folder something like this: 我在计算机上使用MAMP,因此要将以下内容设置为“根”文件夹:

$root = "localhost:1234/project/";

I am trying to do this so I can use this in a file, such as file.php that for example is in a deeper lever like 我正在尝试执行此操作,因此可以在文件(例如file.php)中使用它,例如,

localhost:1234/project/folder1/folder2/file.php

and then inside the file I can maybe require/include another file from the "root" file, such as: 然后在文件中,我可能需要/包括“根”文件中的另一个文件,例如:

include $root . "/functions.php";

or deeper within a level 或更深一层

include $root . "/folderx/another.php";

Does anybody know how to do this? 有人知道怎么做这个吗?

I tried dirname(__FILE__); 我尝试了dirname(__FILE__); but it doesnt really work. 但它并没有真正起作用。

Thanks! 谢谢!

just remove the slash from your root link 只需从根链接中删除斜杠
$root = "localhost:1234/project/";
to $root = "localhost:1234/project"; $root = "localhost:1234/project";
and include your file like this 并包含这样的文件
include $root . "/functions.php";

or 要么
keep your root and require the files without slash 保持根目录,并要求文件不加斜线

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

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