PHP 文件导航(本地和远程)
我一直在开发内容管理系统(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
总的来说,我觉得还不错。
您可能想给变量赋予更多的名称(例如
nakid_root_dir
、nakid_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 tostr_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.
我不完全理解你想要得到什么,但也许 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