SGUserFace Posted July 21, 2016 Share Posted July 21, 2016 How to write correctly path in include function? Quote Link to comment https://forums.phpfreaks.com/topic/301535-how-to-write-correctly-path-in-include-function/ Share on other sites More sharing options...
Jacques1 Posted July 21, 2016 Share Posted July 21, 2016 Write a coherent problem description (preferably with actual code), then we might be able to help you. Quote Link to comment https://forums.phpfreaks.com/topic/301535-how-to-write-correctly-path-in-include-function/#findComment-1534791 Share on other sites More sharing options...
ginerjm Posted July 21, 2016 Share Posted July 21, 2016 (edited) Just realize that burying a specific path inside a function limits its portability. If there has to be a path why not pass it in as an argument? Or are you referring to the include STATEMENT, and not a user-written function? If so, I think you can answer that yourself. Edited July 21, 2016 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/301535-how-to-write-correctly-path-in-include-function/#findComment-1534792 Share on other sites More sharing options...
SGUserFace Posted July 24, 2016 Author Share Posted July 24, 2016 Write a coherent problem description (preferably with actual code), then we might be able to help you. How to get the root directory of project I dont want to use any time ../ include(What is the root? . 'images/default.png'); Quote Link to comment https://forums.phpfreaks.com/topic/301535-how-to-write-correctly-path-in-include-function/#findComment-1534932 Share on other sites More sharing options...
ginerjm Posted July 24, 2016 Share Posted July 24, 2016 And again - you want this in a function? It s/b passed in as part of the parms. BTW - you want to use an include statement for a .png file? Makes no sense. Quote Link to comment https://forums.phpfreaks.com/topic/301535-how-to-write-correctly-path-in-include-function/#findComment-1534934 Share on other sites More sharing options...
Jacques1 Posted July 24, 2016 Share Posted July 24, 2016 There is no magical function for getting the project root, because PHP doesn't know that path. What you can do is have the webserver set an environment variable with the project root (how exactly that works depends on your webserver; google it). PHP has then access to that variable: require_once $_SERVER['MY_PROJECT_ROOT'].'/path/to/script.php'; Alternatively, you could define a constant with the path within PHP (e. g. in a configuration script). But that means you always need to load the initial script with an “ugly” path before you can use the constant for everything else: <?php define('MY_PROJECT_ROOT', $_SERVER['DOCUMENT_ROOT'].'/path/to/project/'); <?php require_once __DIR__.'/../relative/path/to/config.php' require_once MY_PROJECT_ROOT.'/path/to/some/script.php'; // now MY_PROJECT_ROOT is available 1 Quote Link to comment https://forums.phpfreaks.com/topic/301535-how-to-write-correctly-path-in-include-function/#findComment-1534937 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.