我正在用 PHP 构建我的第一个网站。我正在本地测试它,并在两台计算机上进行开发,我的家用计算机和我的 Uni 计算机,主要项目存储在 Dropbox 中。
该项目在两台计算机上具有不同的根路径,因为它们具有不同的用户名。
'/home/vadar/Dropbox/ENELWebsite/public_html/'
'/home/tcpb1/Dropbox/ENELWebsite/public_html/'
这显然已经成为一个问题,因为使用根路径包含文件将导致一台计算机出现错误。
我最初尝试将它包含在我的索引页面中,但它还需要包含它的包含文件的根路径,我必须在计算机之间更改它。
$dir_home = '/home/vadar/Dropbox/ENELWebsite/public_html/';
$dir_uni = '/home/tcpb1/Dropbox/ENELWebsite/public_html/';
if(file_exists($dir_home) OR is_dir($dir_home)){
define('APP_DIR', $dir_home); //home comp
}
elseif(file_exists($dir_uni) OR is_dir($dir_uni)){
define('APP_DIR', $dir_uni); //uni comp
}
else{
echo 'No include directory exists';
}
解决这个问题最聪明的方法是什么?我意识到我可以在其中任何一个上创建一个新的用户文件夹,然后将 Dropbox 文件夹移到其中,但我确信有一种更智能的方法可以将文件包含在项目本身中。
最佳答案
使用$_SERVER['DOCUMENT_ROOT']找到根文件夹。
$dir = $_SERVER['DOCUMENT_ROOT']; // will return /home/vadar/Dropbox/ENELWebsite/public_html/
if(file_exists($dir)){ //just to make sure
define('APP_DIR', $dir); //home comp
}
else{
echo 'No include directory exists';
}
关于php - 如何包含文件以便项目在多个开发环境中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13889021/