簡體   English   中英

使用include獲取php中的根文件夾路徑

[英]Get the root folder path in php using include

我在 c:\wamp\www\ 中有名為mywebsite的 php Web 應用程序。 該文件夾由features/fashionWeek/database/Mysql.php文件組成。 我使用 dirname( FILE ) 函數從根目錄獲取文件夾路徑,以便可以將文件夾放置到實時服務器的 public_html 文件夾中的任何位置。

它在我的本地主機中運行良好。 我在 wamp\www\mywebsite\featured\fashionWeek\database\Mysql.php 中有以下代碼 include(dirname( FILE ); 工作正常,返回此路徑:C:/wamp/www/mywebsite/featured/fashionWeek/database

<?php  include(dirname(__FILE__).'/../config/Dbconfig.php') ;?>

但是當我將 mywebsite 文件夾放在實時服務器的 public_html 文件夾下時。 它給出了以下錯誤:

Warning: include(/home/websitename/public_html/featured/fashionWeek/database/../config/Dbconfig.php) [function.include]: failed to open stream: No such file or directory in /home/websitename/public_html/featured/fashionWeek/database/Mysql.php on line 3

Fatal error: include() [function.include]: Failed opening required '/home/websitename/public_html/featured/fashionWeek/database/../config/Dbconfig.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/websitename/public_html/featured/fashionWeek/database/Mysql.php on line 3

要倒退,即“..”,您可以使用dirname函數。

<?php
include(dirname(dirname(__FILE__)).'/config/Dbconfig.php');

應該管用。

更多的

最終管理這些會很煩人,所以我建議在您的原始文件中定義一個 ROOT 常量。 這將包含項目根目錄的絕對路徑。 這很簡單。

defined('ROOT') or define('ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR);

https://stackoverflow.com/a/2152320/1528331

看看是否有幫助。 還,

dirname(__FILE__) is the same as __DIR__

有點松弛,但它可以幫助你

if(!function_exists('getRootdir'))
{
function getRootdir($NFILE)
{
$filefitter="";
while(!file_exists($filefitter.$NFILE))
$filefitter="../".$filefitter;
return $filefitter;
}
}

利用

like  $rootpath=getRootdir("Root identifier folder / php file");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM