第一个和第二个有require语句,关键是其中的第二个文件中的require语句的require不是根据他的文件位置出发的相对路径,而是根据第一个文件的相对路径(因为第一个文件用了require来包含第二个文件!),我其他目录夹里的文件就不能正确调用这第二个文件了,因为路径会出错!-----------------------------------------------it won't happen. the require function in the second page uses the directory relative to the second page.
Warning: require_once(../util/die_out.php) [function.require-once]: failed to open stream: No such file or directory in C:\ApacheGroup\Apache2\www\mywan\class\Gamer.class.php on line 3我要上一层目录,所以用了../util/die_out.php
做了一个函数<?php /* * Created on 2006-6-13 * * To change the template for this generated file go to * Window - Preferences - PHPeclipse - PHP - Code Templates */ function smarty_require($path,$abs_path){ preg_match("/([.|\/]*)(.*)/",$path,$matches); //var_dump($path);
require("dir/file.php");
这样可以。但如果用
require("./file.php");
这样就不行。
http://community.csdn.net/Expert/topic/4648/4648044.xml?temp=.8632013
require("file.php");
require("dir/file.php");
这样可以。但如果用
require("./file.php");
这样就不行。Gdj你可能没有看清我的问题,我的问题是有三个文件第一个和第二个有require语句,关键是其中的第二个文件中的require语句的require不是根据他的文件位置出发的相对路径,而是根据第一个文件的相对路径(因为第一个文件用了require来包含第二个文件!),我其他目录夹里的文件就不能正确调用这第二个文件了,因为路径会出错!
2、如果需要的这样的引用,可以以__FILE__作为脚本基本路径进行引用。
the require function in the second page uses the directory relative to the second page.
/*
* Created on 2006-6-13
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
function smarty_require($path,$abs_path){
preg_match("/([.|\/]*)(.*)/",$path,$matches);
//var_dump($path);
$link=split("\\\\", $abs_path);
//var_dump($link);
if($matches[1]){
$step=split("/", $matches[1]);
}
//var_dump($matches[1]."<br>");
//var_dump($step);
$length=-1;
$max=count($step);
for($i=0;$i<$max;$i++){
if($step[$i]=='..'){
$length--;
}
}
$link=array_slice($link,0,$length);
$result=implode("/",$link);
echo ($result);
if($matches[2]){
$result=$result."/".$matches[2];
}
else{
die("路径输入有误!");
}
require_once($result);
}
?>
smarty_require("../util/die_out.php",__FILE__); //这样调用!但现在不知道如何能不能象php内置函数一样直接调用!
require("../xx.php");
改成这样就行了
require(dirname(__FILE__)."/../xx.php");