Jump to content

How does PHP know to up or go down?


ChenXiu

Recommended Posts

Why does the following scenario work:
index.php is down in the public_html directory.
index.php requires "secret.php" which is up in the root directory.
How come putting these lines of code at the top of secret.php (which is up in the root directory) both work?
<?php
require 'secret_2.php';  <--- file alongside of secret.php (notice no dot-dot-slash ../)
require 'index_2.php'; <--- file alongside of index.php back down in public_html directory (notice no dot-dot-slash ../)

How does PHP know to look in the same directory, or jump down to public_html?

Thanks.

Link to comment
Share on other sites

31 minutes ago, ChenXiu said:

How does PHP know to look in the same directory, or jump down to public_html?

It doesn't.  If you don't provide a path when including a file, php will search for the file in the locations defined in the include_path.  I'd guess that your public_html directory is part of this.

var_dump(get_include_path());

 

  • Like 1
Link to comment
Share on other sites

It doesn't unless you tell it. There are two ways to specify a file location, absolute and relative. If the path starts with a '/' then the path is absolute, otherwise it is relative to the document root. If just the filename is specified then it expects it to be in the document root. If you want it to look "down" then specify the directory without a leadlng '/'.

Link to comment
Share on other sites

5 minutes ago, Barand said:

The manual explains it.

I read that -- php checks the scripts own working directory, and then the current working directory before failing -- but I thought it would give an E_Warning if it had to look in more than two places? I have all errors turned on, but no warnings were issued. Very strange I think.

Link to comment
Share on other sites

This is one reason some prefer to use absolute paths when including files, another is that it will be more adaptable than relying on include paths.

The idea is that you store the "base path" of your active project/script, which you can then use to work out the paths for the rest of your files — either as a global constant or a  variable you pass around as needed.

From PHP 5.5 and onwards, the following should work:
 

define('BASE_PATH', rtrim(preg_replace('#[/\\\\]{1,}#', '/', __DIR__), '/') . '/');

 

  • Thanks 1
Link to comment
Share on other sites

22 hours ago, Barand said:

Read it again

Yep, you are right. An error only if it can't find it in any of the places it looks.
It's somewhat disconcerting to know that PHP has to look for something in more than one place.

5 hours ago, JacobSeated said:

This is one reason some prefer to use absolute paths

Thank you! That sounds like the best idea.
LOL -- I was on vacation when they taught "BASE_PATH" and "__DIR__ " and even "define." Seriously.
I feel REALLY DUMB not knowing all that stuff.
Just about ALL php code written these days has words like define, public, static, class, namespace, use, const, private, try, catch, etc., and I have NO clue what they mean or how to use them. It's a miracle my website with its 1990's php code is way faster, error-proof, and more efficient than over 90% of today's websites I visit.
My biggie learning project today is to learn how to indent 😃

Edited by ChenXiu
Link to comment
Share on other sites

10 hours ago, JacobSeated said:

From PHP 5.5 and onwards, the following should work:
 

define('BASE_PATH', rtrim(preg_replace('#[/\\\\]{1,}#', '/', __DIR__), '/') . '/');

 

That's overkill: PHP doesn't care whether your path uses backslashes (for Windows only, of course) or forward slashes, and __DIR__ isn't going to have doubled slashes that requires regular expressions to replace.

All you need is

const BASE_PATH = __DIR__;

 

Link to comment
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.