Jump to content


Photo

Issue with relative paths & includes


  • Please log in to reply
4 replies to this topic

#1 bradleyjx

bradleyjx
  • New Members
  • Pip
  • Newbie
  • 2 posts
  • LocationFuedal Japan

Posted 01 August 2006 - 09:15 PM

Alright, I'm building a PHP web mini-app that I need to have able to be moved around in such a way that I am forced to use relative paths for almost everything. I am having issues with using these paths and getting them to work.

Example: (simplified to handle the specific problem I'm having)

I have two files that I want to connect via an include statement. One is in /foo/bar/file.php, while the other is in /include/header.php. So, I'm using the following script in file.php to include header.php:

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

I've found that the problem I'm having has to do with the "../", either by me doing something that isn't right or by some other reason.


So, my question is whether or not that include statement is doing anything that I'm not expecting it to do? If it is written the way it's supposed to, what could be wrong, as in my code the file does not get included into the web page.
-bjx

#2 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 01 August 2006 - 09:21 PM

try changing the single quotes around the file's path to double quotes.  PHP will assume that '../' literally means ../, rather than the previous directory:

include("../../include/header.php");

can't guarantee this is the problem, but give that a shot.

#3 bradleyjx

bradleyjx
  • New Members
  • Pip
  • Newbie
  • 2 posts
  • LocationFuedal Japan

Posted 03 August 2006 - 05:34 PM

@akitchen: I've tried almost every iteration of using single and double quotes, as well as preceding the path with "/" and trying to use the path as if "../" went to the server root rather than up one directory.


Let me clarify what's happening (after some more testing and figuring out why some things work and some don't):

Here's the directory I'm using to test this:

/a/
/a/foo
/a/foo/bar
/a/foo/baz
/a/one/two

Each folder contains a php file with the same name as the folder it is contained in. (so the file in /foo/bar is called bar.php) In addition, there is an index.php file located in /foo/bar

Now, the following two include statements work from index.php...

include('bar.php');
include('../foo.php');

In addition, the following two include statements work as well. (they have very unneccessary code, but they show that things that don't work later seem to work in these special cases)...

include('../bar/bar.php');
include('../../foo/bar/bar.php');

...and none of the following statements work from index.php...

include('../bar/bar.php');
include('../../a.php');
include('../../one/one.php');
include('../../one/two/two.php');



Any ideas?
-bjx

#4 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 04 August 2006 - 01:03 PM

If you create a file in your includes folder, with variables set to the definate path to your files, then from where ever you are you can require once the file in your includes folder, and call the variables that contain your files and they should run. i.e.

$foo = '/a/foo/foo.php';
$bar = '/a/bar/bar.php';

In your includes folder, then from your file in httpd

include($foo);
include($bar);

Then no-one can hack your file locations cos they don't have permissions, yeah.


#5 Vikas Jayna

Vikas Jayna
  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts
  • LocationNoida, Delhi, India

Posted 04 August 2006 - 04:35 PM

I have experienced a similar sort of a problem when we had to shift some scripts from one machine to another. On the new machine the relative paths did not work with includes. I later found that that's because of the php version. This sort of problem appears in the php cli version whereas the cgi version works fine. Could you check yours?
php -v
Vikas Jayna,
Project Manager - Jeevansathi.com
7 yrs. exp. in LAMP
Certified Mysql DBA
Zend Certified Engineer




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users