ChenXiu Posted August 11, 2021 Share Posted August 11, 2021 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?<?phprequire '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. Quote Link to comment Share on other sites More sharing options...
kicken Posted August 11, 2021 Share Posted August 11, 2021 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()); 1 Quote Link to comment Share on other sites More sharing options...
gw1500se Posted August 11, 2021 Share Posted August 11, 2021 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 '/'. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 11, 2021 Share Posted August 11, 2021 The manual explains it. Quote Link to comment Share on other sites More sharing options...
ChenXiu Posted August 11, 2021 Author Share Posted August 11, 2021 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 11, 2021 Share Posted August 11, 2021 1 hour ago, ChenXiu said: but I thought it would give an E_Warning if it had to look in more than two places Read it again. It says it gives a warning if it isn't found (or isn't accessible). What is your error_reporting level? 1 Quote Link to comment Share on other sites More sharing options...
JacobSeated Posted August 12, 2021 Share Posted August 12, 2021 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__), '/') . '/'); 1 Quote Link to comment Share on other sites More sharing options...
ChenXiu Posted August 12, 2021 Author Share Posted August 12, 2021 (edited) 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 August 12, 2021 by ChenXiu Quote Link to comment Share on other sites More sharing options...
Barand Posted August 12, 2021 Share Posted August 12, 2021 45 minutes ago, ChenXiu said: It's somewhat disconcerting to know that PHP has to look for something in more than one place. I find it comforting to know that it will try. Quote Link to comment Share on other sites More sharing options...
benanamen Posted August 12, 2021 Share Posted August 12, 2021 46 minutes ago, ChenXiu said: My biggie learning project today is to learn how to indent Any decent editor or IDE will do it for you. Quote Link to comment Share on other sites More sharing options...
requinix Posted August 12, 2021 Share Posted August 12, 2021 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__; 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.