mrbean Posted June 3, 2011 Share Posted June 3, 2011 Hello there, I think its an easy question to answer but I don't know the answer myself Question: I have for example 3 files: index.php (in root directory) test.php(in 'includes' directory) test2.php(in 'includes' directory) The script looks like this: Index.php <?php include("includes/test.php"); ?> test.php <?php include("test2.php"); ?> test2.php class function class class something etc. Just some code Now when I include test.php in index.php, test.php will think I want to include an file in the root directory(where index.php also is) But I want to include the file where test.php and test2.php both are("includes" directory/path). So include path to path of file. How can I do that? I hope u understand me (I speak bad english) Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 3, 2011 Share Posted June 3, 2011 since the folder is called 'includes' I'm assuming the files in there are always pulled out by another file in the root directory and never actually executed from there they reside. you can simply use include('includes/test2.php'); in test.php Quote Link to comment Share on other sites More sharing options...
requinix Posted June 4, 2011 Share Posted June 4, 2011 To elaborate, include() and friends include files relative to the current working directory. In 99% of cases that's the directory with the PHP script that first executed - not the directory with the script executing at that moment (ie, the one with the include()). Two easy ways to make sure you include the right files involve using absolute paths: // relative to the root of the website include $_SERVER["DOCUMENT_ROOT"] . "/includes/test2.php"; // relative to the current file include __DIR__ . "/test2.php"; // PHP 5.3+ only include dirname(__FILE__) . "/test2.php"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.