waynew Posted October 13, 2008 Share Posted October 13, 2008 Ok, I've never needed to use this until now. :\ And Google seems to give me everything but the right result: How do I get the absolute path to something. I need this because I'll be reusing the same file in multiple directories and I reckon that changing every link to every file manually is going to leave me in a world of hurt. Quote Link to comment https://forums.phpfreaks.com/topic/128193-absolute-path/ Share on other sites More sharing options...
DarkWater Posted October 13, 2008 Share Posted October 13, 2008 http://us.php.net/realpath =P Quote Link to comment https://forums.phpfreaks.com/topic/128193-absolute-path/#findComment-663925 Share on other sites More sharing options...
waynew Posted October 13, 2008 Author Share Posted October 13, 2008 Damn you and your uber Google skills. Quote Link to comment https://forums.phpfreaks.com/topic/128193-absolute-path/#findComment-663926 Share on other sites More sharing options...
waynew Posted October 13, 2008 Author Share Posted October 13, 2008 Found an article. It suggests something different. http://roshanbh.com.np/2008/01/absolute-path-and-relative-path-file-inclusion-in-php.html What do you reckon? Bearing in mind that I'm working on my local server at the moment. Quote Link to comment https://forums.phpfreaks.com/topic/128193-absolute-path/#findComment-663927 Share on other sites More sharing options...
DarkWater Posted October 13, 2008 Share Posted October 13, 2008 Damn you and your uber manual searching skillz. Corrected. Quote Link to comment https://forums.phpfreaks.com/topic/128193-absolute-path/#findComment-663928 Share on other sites More sharing options...
waynew Posted October 13, 2008 Author Share Posted October 13, 2008 It ain't a skill unless it has a "z" at the end. Quote Link to comment https://forums.phpfreaks.com/topic/128193-absolute-path/#findComment-663930 Share on other sites More sharing options...
DarkWater Posted October 13, 2008 Share Posted October 13, 2008 Found an article. It suggests something different. http://roshanbh.com.np/2008/01/absolute-path-and-relative-path-file-inclusion-in-php.html What do you reckon? Bearing in mind that I'm working on my local server at the moment. I think you guyz must be aware of absolute path and relative path in PHP. If you do not know anything about it then let me explain you about absolute path and relative path in the server with the example using include() function of PHP. You can see the example below, I’ve included the same files in PHP but the same file is included in two different manner. That bad grammar made my day. And in applications, I always set up a bootstrapping file that I always include relatively. It sets everything else up. Example <?php $homedir = '/home1/phpspeak'; $appdir = "$homedir/public_html/assignments"; function __autoload($classname) { $path = '/home1/phpspeak/public_html/assignments/classes/' . $classname . '.class.php'; if (file_exists($path)) { require_once ($path); } } $mysqlu = ""; $mysqlp = "" $mysqld = ""; $mysqlh = ""; $db = new mysqli($mysqlh, $mysqlu, $mysqlp, $mysqld); if ($db->connect_error) { printf("Connect failed: %s<br />\n", mysqli_connect_error()); exit(); } require ("$homedir/smarty/Smarty.class.php"); $template = new Smarty(); //Set up directories $template->template_dir = "$homedir/smarty/templates"; $template->compile_dir = "$homedir/smarty/templates_c"; $template->cache_dir = "$homedir/smarty/cache"; $template->config_dir = "$homedir/smarty/configs"; $notes = new Note; $registry = new Registry; $registry->db = $db; $registry->template = $template; $registry->notes = $notes; The only reason I hardcoded $homedir is because I felt like putting Smarty in a directory outside of the webroot. Otherwise I would have used realpath() to get $homedir. Quote Link to comment https://forums.phpfreaks.com/topic/128193-absolute-path/#findComment-663934 Share on other sites More sharing options...
PFMaBiSmAd Posted October 13, 2008 Share Posted October 13, 2008 Are you talking about a file system path or a URL (i.e. link). They are two different things. To form an absolute file system path for an include/require, use - require $_SERVER['DOCUMENT_ROOT'] . "/path_to_file/your_file.php"; Quote Link to comment https://forums.phpfreaks.com/topic/128193-absolute-path/#findComment-663963 Share on other sites More sharing options...
waynew Posted October 13, 2008 Author Share Posted October 13, 2008 Whoops. I have a master file that includes another file that includes a bunch of classes. I used: require(realpath("include_me/classes/classes.php")); Bearing in mind that I include this master file at the top of all my pages; here's the error: Fatal error: require() [function.require]: Failed opening required '' (include_path='.;C:\php5\pear') in C:\Program Files\Apache Group\Apache2\htdocs\waynewhitty02\include_me\master\master.php on line 13 Quote Link to comment https://forums.phpfreaks.com/topic/128193-absolute-path/#findComment-663969 Share on other sites More sharing options...
waynew Posted October 13, 2008 Author Share Posted October 13, 2008 I tried using: $root = $_SERVER['DOCUMENT_ROOT']; require($root."include_me/classes/classes.php"); But I still get: Warning: require(C:/Program Files/Apache Group/Apache2/htdocs/include_me/classes/classes.php) [function.require]: failed to open stream: No such file or directory in C:\Program Files\Apache Group\Apache2\htdocs\waynewhitty02\include_me\master\master.php on line 13 Bear in mind that master.php is held inside a folder called master, which is inside the include_me folder. Quote Link to comment https://forums.phpfreaks.com/topic/128193-absolute-path/#findComment-663976 Share on other sites More sharing options...
PFMaBiSmAd Posted October 13, 2008 Share Posted October 13, 2008 C:/Program Files/Apache Group/Apache2/htdocs/include_me/classes/classes.php If that did not work, then either that path is wrong of the file name does not exist in that folder - No such file or directory Quote Link to comment https://forums.phpfreaks.com/topic/128193-absolute-path/#findComment-663983 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.