PHP获取文件目录dirname(__FILE__),getcwd()

时间:2022-06-02 00:17:07
以discuz x2.5为例
D:/www/upload2.5/test.php
D:/www/upload2.5/source/class/class_test.php

test.php文件如下
<?php  
define('APPTYPEID', 2);
define('CURSCRIPT', 'test');
//by http://www.manongjc.com/article/1321.html
require './source/class/class_core.php';

C::app()->init();

echo DISCUZ_ROOT."<br/>";

echo __FILE__."<br/>";
echo dirname(__FILE__)."<br/>";
$dir = getcwd();echo $dir;

echo "<hr/>";

include DISCUZ_ROOT."source/class/class_test.php";
?>

class_test.php文件如下

<?php  

echo __FILE__."<br/>";
echo dirname(__FILE__)."<br/>";
$dir2 = getcwd();echo $dir2;
//by http://www.manongjc.com/article/1321.html
?>
运行结果如下
D:\www\upload2.5\                             -->DISCUZ_ROOT
D:\www\upload2.5\test.php -->__FILE__
D:\www\upload2.5 -->dirname(__FILE__)
D:\www\upload2.5 -->getcwd()


D:\www\upload2.5\source\class\class_test.php -->__FILE__
D:\www\upload2.5\source\class -->dirname(__FILE__)
D:\www\upload2.5 -->getcwd()
由此可见
__FILE__代表所在文件的路径
getcwd()运行文件的所在目录


DISCUZ_X2.5根路径是这样创建的
所在文件D:\www\upload2.5\source\class\class_core.php
define('IN_DISCUZ', true);  
define('DISCUZ_ROOT', substr(dirname(__FILE__), 0, -12));

Magento根路径创建如下
根目录index.php中
define('MAGENTO_ROOT', getcwd());