Jump to content

Recommended Posts

I've made it my personal mission to point out this issue:

instead of just config.php in the same directory as the code requiring it is in.

It's not relative to the file that's doing the requiring. It's relative to the current working directory. Generally it is the same directory as the first file that was executed, but not necessarily. Generally it doesn't change, but not necessarily.

Link to comment
https://forums.phpfreaks.com/topic/258866-which-is-better/#findComment-1327132
Share on other sites

I don't know if it's just me being illiterate, but this part:

Generally it doesn't change, but not necessarily. confuses me.

 

I'm now just confused about the points trying to be made.  I'm saying the first part of the code is better, because config.php is in the root directory of his script. So if he did include "config.php";  inside a file in, /root/folder/, and config.php is in /root/ it would fail. if the root path passed onto /root/folder/file.php and it was included within that file, it would work.

 

You are saying if he did include "config.php" in any file in any directory, it would try to include config.php from whatever directory you were in( the current working directory ). So if you're in /root/ and have an include there it'll check /root/, as to where if you were in /root/folder/ it would search there for the file. So I don't see how it's not relative to the files path with the file that is doing the include.  If index.php is in /root/ and has include "config.php" it will search /root/ for that file, which would mean it is relative.

 

Anyway, I'm not trying to start an argument or be a douche, I am just obviously confused by what you meant.

Link to comment
https://forums.phpfreaks.com/topic/258866-which-is-better/#findComment-1327134
Share on other sites

Huh. If you could see me now I'd have egg on my face :-[ It seems they've changed the behavior of include() and its friends to what everybody's expected they did. Now it does search the current file's directory at some point in the process and after it tries the CWD.

In my defense I know I'm correct... but apparently for previous versions of PHP before 5.3.6 (what I have on-hand to test with). And going through the user comments, for starters, reaffirms that it's changed in the relatively recent past.

 

In other words, DISREGARD THAT I SUCK EGGS.

 

But it's still best to use absolute paths. That hasn't changed.

Link to comment
https://forums.phpfreaks.com/topic/258866-which-is-better/#findComment-1327142
Share on other sites

It seems they've changed the behavior of include() and its friends to what everybody's expected they did. Now it does search the current file's directory at some point in the process and after it tries the CWD

 

It hasn't changed. Generally, it's a good idea to have . on your include path.

Link to comment
https://forums.phpfreaks.com/topic/258866-which-is-better/#findComment-1327146
Share on other sites

It seems they've changed the behavior of include() and its friends to what everybody's expected they did. Now it does search the current file's directory at some point in the process and after it tries the CWD

 

It hasn't changed. Generally, it's a good idea to have . on your include path.

Not that. That's still relative to the CWD (and I tested this). The fact that it eventually searches the current file's directory regardless of the include_path is new.

Link to comment
https://forums.phpfreaks.com/topic/258866-which-is-better/#findComment-1327354
Share on other sites

I don't know if it's just me being illiterate, but this part:

Generally it doesn't change, but not necessarily. confuses me.

 

 

The point being made is that the current working directory is not necessarily the same as the directory that contains the file being run.  It can be changed either through the use of chdir or by how you run your script.  For instance in PHP-CLI, lets assume the following layout:

/root/myscripts/script.php

/root/myscripts/config.php

 

And your currently sitting in the /root directory.  When you run php myscripts/script.php then the current working directory is /root, not /root/myscripts.  As such, a relative path of "config.php" would be incorrect and cause a file not found error, where as using __FILE__ to generate an absolute path would work.

 

As has been noted though, PHP's include apparently will take this into account on it's own and locates a file using a process like:

Does the given path exist? Yes, Include it

Can we find it by searching include_path?  Yes, include it

Does it exist in getcwd()? Yes, include it

Does it exist in __DIR__? Yes, include it

Else, fail.

 

That is specific to include/require though.  If you were to do something like fopen or file_get_contents it would fail with a file does not exist error.

 

Link to comment
https://forums.phpfreaks.com/topic/258866-which-is-better/#findComment-1327378
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.