包括使用直接路径而不是相对路径dbconfig.php文件失败
我正在开发一个应用程序并使用一个子文件夹作为测试域。包括使用直接路径而不是相对路径dbconfig.php文件失败
http://mywebsite/testing-folder
我DBCONFIG位于:
http://mywebsite/testing-folder/lib/dbconfig.php
,如果我包括文件: http://mywebsite/testing-folder/userarea/profile
它是罚款的相对路径。如果我将它包含在直接路径中,则会引发错误:
FATAL ERROR: Call to a member function prepare() on a non-object in...
$root = 'http://mywebsite/testing-folder/';
include '../../lib/dbconfig.php'; // This works
include $root.'lib/dbconfig.php'; // This does not
有什么线索为什么?
您试图通过HTTP包含文件。这是不对的,你应该包含文件,将本地文件系统路径传递到include
或require
。
你的根或许应该像这样:
$root = '/var/www/mywebsite/testing-folder/';
谢谢..这没关系! –
除了使用URL的,你必须使用一个文件系统路径。因此,而不是
$root = 'http://mywebsite/testing-folder/';
像
$root = '/var/www/whatever/';
应该被使用。当你通过http包含php时,你的web服务器将已经解释代码并返回你输出的任何内容(如echo "hello world";
)。如果您的文件只包含配置,则输出可能为空。
谢谢!必须接受第一个来的人。 –
虽然这是一个回答!这说35分钟前,而接受的答案是38分钟前。 – Jamesking56
请阅读本文,您将找到答案:http://www.php.net/manual/en/function.include.php#52342 – vee