简体   繁体   中英

PHP Include/Require Relative path from WebRoot Issues

I've done a ton of programming in many languages but newer to hardcore PHP. After tons of googling i can't seem to find a good solultion to include/require/require_once using a relative path from webroot rather than relative path from OS.

I adjusted the PHP Ini include path which was a great solution but the problem is that many PHP IDE's don't evaluate the Include Path in order to expose Class definitions for intellisense etc... So i find myself having to make many temporary includes while debugging/coding.

Classic ASP and other languages to me made this so much easier with relative pathing always being relative from web root rather than php's relative of current file location and php's root being root of the file system. I have seen tons of quirky weird methods to try to work around this but that is exactly what it always seems to be a work around.

Can any of you help make my week and solve this tedius irritating challenge for me?

You can always add something like this to your php.ini file:

ROOT_PATH = "/var/www/<your_directory>"

Then you could pick up this value in your php page like:

$ROOT_PATH = ini_get("ROOT_PATH");

And then you could include files like this:

include_once($ROOT_PATH."/<path>/<filename.php>");

Of course you could simply add define("ROOT_PATH", "/var/www/<your_directory>"); directly in the file, but if you get the settings from the php.ini you could have different settings on different servers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM