Jump to content

Recommended Posts

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/128193-absolute-path/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/128193-absolute-path/#findComment-663934
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/128193-absolute-path/#findComment-663969
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/128193-absolute-path/#findComment-663976
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.