Jump to content

Archived

This topic is now archived and is closed to further replies.

bradleyjx

Issue with relative paths & includes

Recommended Posts

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:

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

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.

Share this post


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

[code]include("../../include/header.php");[/code]

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

Share this post


Link to post
Share on other sites
@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...

[code]include('bar.php');[/code]
[code]include('../foo.php');[/code]

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)...

[code]include('../bar/bar.php');[/code]
[code]include('../../foo/bar/bar.php');[/code]

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

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



Any ideas?

Share this post


Link to post
Share on other sites
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.

[code]
$foo = '/a/foo/foo.php';
$bar = '/a/bar/bar.php';
[/code]

In your includes folder, then from your file in httpd

[code]
include($foo);
include($bar);
[/code]

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.