Jump to content

[SOLVED] absolute path in php


chronister

Recommended Posts

Hi Folks,

 

I got a sorta dumb question. I am trying to do an include(). Problem is, I cannot figure out how to define the absolute path. In HTML you can simply use /folder1/folder2/file.ext and no matter where you are in the hierarchy, the / denotes to start at the root of the site root. What is the equivalent in PHP.

 

I have tried include('/path/to/file');, include('path/to/file'); and include('../path/to/file'); and include('./path/to/file');. It works in some places, but as soon as I go outside that structure it gives me errors.

 

Thanks in advance.

 

Nate

Link to comment
Share on other sites

ok, if your file resides in the same folder as your script - as such

 

Folder Tree

 

www |_dbconnect.php

public_html |_ index.php

    |        |_ includes.php

    |_____includes - functions.php

    |_____includes - common.inc.php

    |_____includes - dbconnect.php

 

 

Ok, now we have our tree - lets say index.php calls includes.php

 

The code would be include('includes.php'); (same directory)

 

Now includes.php calls the 3 files in the includes folder

So one by one the call would be include('/includes/functions.php')';

 

to include a file in a directory above its own, use ../ for each directory level

Link to comment
Share on other sites

Right, I understand that. I use a standard header and footer so that my pages all open in a content area in header.php

 

/header.php

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php include('etc/includes.php');?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title></head><body>

<table width="100%" height="257" border="0" cellpadding="0" cellspacing="0">
  <tr><td height="75" colspan="2">header</td></tr>
  <tr><td width="23%">Menu</td><td width="77%">

       Dynamic Content Area -- all pages load here

<?php function footer(){ ?>	 
</td></tr>  
<tr><td height="10" colspan="2">Footer</td></tr></table>
</body>
</html>
<?php } ?>
  

 

The rest of my pages look like this

/folder1/file.php

<?php include('../header.php'); ?>

Some content here

<?php footer(); ?>

 

Now most of my include files are in /etc and are included in my main includes file

 

/etc/include.php

<?php
global $message,$color1,$color2,$begin,$end,$total_entries;

include('library_include.php');
include('address_book_include.php');
include('movie_include.php');
include('stitch_include.php');
include('shopping_list_include.php');
include('recipe_inc.php');
include('repeat_event_functions.php'); 
include('calendar_include.php');


?>

 

I would like to include a file that is not in this directory and I essentially need to specify that the include path starts at the root. Kinda like if you specify image src as /images. Even if your in /folder1/folder2/folder3/file.php, the page starts at the root and goes to images directory first. I am looking for the equiv in php. If I use ../included_file.php, then when I am in the root directory (index.php) I get an error because it cannot go back a level to find the file. If I use include('included_file.php') The script looks in the current directory.

 

Is there a way to specify to start at root no matter where the page is located at?

 

Link to comment
Share on other sites

You will have to define this variable yourself. Assuming your server is set up like most linux servers put this in one of your config.php files (I am using fedora as an example here):

 

Say the config file is in your public_html dir, and the path from root to your public_html folder is like so:

/home/user/public_html

 

$s_root = '../../../../etc/';

 

You have to first escape out of public_html, with ../, then out of user with another ../ then out of home with another ../, then into the root folder with another ../, then the path/to/yourfile.php

 

Then everytime you need to access etc, use:

 

include($s_root. 'filename.php'); or something like that, keeping in mind, $s_root will be the path from the file that is executing the script, not the config.php file

Link to comment
Share on other sites

  • 3 months later...

Sorry to re-hash this old topic, but I have found a solution to what I wanted and thought I'd share as it may help someone else.

 

The php 'equiv' of /start/at/root is $_SERVER['DOCUMENT_ROOT']

 

 

I have now found that I can place includes in any directory and call them whenever I want by using this.

 

e.g.

 

file.php lives in /folder1/folder2/folder3/file.php

 

by calling it as

<?php include($_SERVER['DOCUMENT_ROOT'].'/folder1/folder2/folder3/file.php') ?>

I have access to this file no matter what the current working directory is. I can call it in a file that is in root or 15 levels deep and it is still starting from ROOT to find the file.

 

Hope it helps someone else.

Link to comment
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.