tags:

views:

31

answers:

1

Is this the right way to go about referencing a file in a parent directory relative to a php source file?:

require_once('../referenced_file.php');
+4  A: 

To include a file relative to the current source file you should use the full path:

require_once dirname(__FILE__) . '/../referenced_file.php';

As the current working directory is set to the directory of the script that was initially executed.

Yacoby
You don't know the working directory for sure. it can be sub-include and will lead to wrong path as well. Everyone is up with code to get precious rep, but noone bother to think before post. I really love SO, A lamer's heaven.
Col. Shrapnel
@Col. This code works fine irrespective of the current working directory. It includes a file in the parent directory relative to the source file (the file that line of code is in).
Yacoby
Yeah. But you cannot be sure that the OP refers to the working file, not parent one. You even cannot be sure it's "file not found" error. But sure, it's not your concern while you get upvotes.
Col. Shrapnel
@Col. From the question post: Is this the right way to go about referencing a file in a parent directory **relative to a php source file?**. Irrespective of what the error is the method in the question isn't the correct method.
Yacoby
@Col. Given that recently on average my answers are 4 days apart and in that time I gave about as many downvotes as upvotes do you think I really give a damn about how high my reputation is? Please don't insult me :)
Yacoby
Ok, I may be wrong this time. I see many automated answers and it makes me sick.
Col. Shrapnel