PHP 文件导航(本地和远程)

发布于 2024-08-19 02:59:17 字数 1014 浏览 1 评论 0原文

我一直在开发内容管理系统(nakid),最严峻的挑战之一是文件导航。我想确保文件路径和设置在本地和远程服务器上有效。现在我的设置几乎是这样的:

first.php(由所有页面使用):

//Set paths to nakid root
$core['dir_cur'] = dirname(__FILE__);
$core['dir_root'] = $_SERVER['DOCUMENT_ROOT'];
//Detect current nakid directory
$get_dirnakid_1 = str_replace("\\","/",dirname(__FILE__));//If on local
$get_dirnakid_2 = str_replace("/includes/php","",$get_dirnakid_1);
$get_dirnakid_3 = str_replace($_SERVER['DOCUMENT_ROOT'],"",$get_dirnakid_2);
//remove first "/"
if(substr($get_dirnakid_3, 0,1) == "/"){
 $get_dirnakid_3 = substr($get_dirnakid_3, 1);
}
//Set some default vars
$core['dir_nakid_path'] = $get_dirnakid_3;
$core['dir_nakid'] = $core['dir_root']."/".$core['dir_nakid_path'];//We need to get system() for this real value - below

我也这样做的原因是因为我希望该程序所在的目录位于服务器上的任何位置,即(/ nakid)(/cms)(/admin/cms)

我确信我做错了事情,或者有一个更简单的方法来处理这一切。

如果有助于仔细查看代码以及如何使用所有内容,我将其全部放在 nakid.org

编辑:刚刚意识到我在 nakid.org 的内容与我新发布的代码有点不同,但相同这个想法仍然适用于我正在尝试做的事情。

I have been working on a content management system (nakid) and one of my toughest challenges is the file navigation. I want to make sure the file paths and settings work on local and remote servers. Right now my setup is pretty much something like this:

first.php (used by all pages):

//Set paths to nakid root
$core['dir_cur'] = dirname(__FILE__);
$core['dir_root'] = $_SERVER['DOCUMENT_ROOT'];
//Detect current nakid directory
$get_dirnakid_1 = str_replace("\\","/",dirname(__FILE__));//If on local
$get_dirnakid_2 = str_replace("/includes/php","",$get_dirnakid_1);
$get_dirnakid_3 = str_replace($_SERVER['DOCUMENT_ROOT'],"",$get_dirnakid_2);
//remove first "/"
if(substr($get_dirnakid_3, 0,1) == "/"){
 $get_dirnakid_3 = substr($get_dirnakid_3, 1);
}
//Set some default vars
$core['dir_nakid_path'] = $get_dirnakid_3;
$core['dir_nakid'] = $core['dir_root']."/".$core['dir_nakid_path'];//We need to get system() for this real value - below

The reason I also did it this way is because I want the directory that this program is sitting in to be anywhere on the server ie(/nakid)(/cms)(/admin/cms)

I'm positive I am doing something the wrong way or that there is a simpler way to take care of all this.

If it helps to get a closer look at the code and how everything is being used I have it all up at nakid.org

EDIT: Just realized what I have at nakid.org is a little different than my newly posted code, but the same idea still applies to what I am attempting to do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁许谁一生繁华 2024-08-26 02:59:17

总的来说,我觉得还不错。

您可能想给变量赋予更多的名称(例如nakid_root_dirnakid_relative_webroot等等。)

记住将\转换为/时路径名称中的:每当将另一个目录名称与其中一个设置匹配时,您需要 str_replace("\\","/"...) 也在那些中。

不过,我不明白您使用 $get_dirnakid_2 的目的是什么。如果我将您的应用程序安装在恰好名为 /etc/includes/php/nakid 的目录中,为什么您会搞砸我的路径?

无论如何,您也应该使这些设置可供用户覆盖。有时,用户可能想要设置与从 DOCUMENT_ROOT 和 consorts 获得的设置不同的设置。

By and large, it looks okay to me.

You might want to give the variables more speaking names (e.g. nakid_root_dir, nakid_relative_webroot and so on.)

Remember when converting \ to / in path names: Whenever you match another directory name to one of those settings, you need to str_replace("\\","/"...) in those too.

I don't understand what you aim at with $get_dirnakid_2, though. Why will you screw up my path if I install your application in a directory that happens to be named /etc/includes/php/nakid?

Anyway, you should make those settings user overwritable as well. Sometimes, the user may want to set different settings from what you get from DOCUMENT_ROOT and consorts.

音盲 2024-08-26 02:59:17

我不完全理解你想要得到什么,但也许 getcwd() 就是你所寻找的:
http://www.php.net/manual/en/function.getcwd。 php

I don't fully understand what you try to get, but maybe getcwd() is what you look for:
http://www.php.net/manual/en/function.getcwd.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文