开发者

PHP path - including a file from the same directory

开发者 https://www.devze.com 2022-12-22 14:21 出处:网络
I have a problem with my path. Say I have a PHP file in /home/bla/www/dev/source/test.php. In this test.php file I want to include a file in

I have a problem with my path. Say I have a PHP file in /home/bla/www/dev/source/test.php. In this test.php file I want to include a file in

/home/bla/www/config/conf.php

<?php
    include_开发者_如何学Pythononce("");
?>

I don't want to include it like include /home/bla/www/config/conf.php. How can I do it?

PS: This fails:

include_once("../../config/conf.php");


I'd suggest to use an absolute path instead of a relative:

include_once($_SERVER['DOCUMENT_ROOT']."/config/conf.php"); 

This would work from any folder.


$current_dir = dirname(__FILE__);
require_once($current_dir.'/../foo/bar.php');

Note that require_once('foo.php') looks for foo.php in the same directory as the script, but require_once('../foo.php') is not relative to the path of the script, but relative to the current working directory.


As DaDaDom suggests, permissions may well be the problem here. Have you tried addressing the file using an absolute path (/home/bla/www/config/conf.php), and if it fails, what's the error?

0

精彩评论

暂无评论...
验证码 换一张
取 消