...

View Full Version : includes and relative paths



maltrecho
07-01-2005, 10:29 AM
:confused: Having the following, why file3A.php is being included while file3B.php is not?:


+ FOLDER1
- file1.php
+ FOLDER2
+ FOLDER3
- file2.php
+ FOLDER5
- file3A.php
+ FOLDER4
-file3B.php



/* file1.php: */

require_once('FOLDER2/FOLDER3/file2.php'); // ok

/* file2.php: */

require_once('FOLDER5/file3A.php'); // right

require_once('../FOLDER4/file3B.php'); //worng

Nightfire
07-01-2005, 12:50 PM
../ Means back a directory. I have no idea where abouts you are in that hierarchy.

maltrecho
07-01-2005, 01:01 PM
../ Means back a directory. I have no idea where abouts you are in that hierarchy.
a) Ok. b) Neither do I, just experimenting...

From the manual:

Files for including are first looked in include_path relative to the current working directory and then in include_path relative to the directory of current script. E.g. if your include_path is ., current working directory is /www/, you included include/a.php and there is include "b.php" in that file, b.php is first looked in /www/ and then in /www/include/. If filename begins with ../, it is looked only in include_path relative to the current working directory.

I just want to find out why the last statement can't include the file.

maltrecho
07-01-2005, 05:46 PM
Oh god, I just realized the answer was written there. Fool me...

[...] If filename begins with ../, it is looked only in include_path relative to the current working directory. [...]

marek_mar
07-01-2005, 10:41 PM
You could play a bit with the realpath() (http://www.php.net/realpath) function.

maltrecho
07-02-2005, 12:59 PM
Having the same hierarchy:


+ FOLDER1
- file1.php
+ FOLDER2
+ FOLDER3
- file2.php
+ FOLDER5
- file3A.php
+ FOLDER4
-file3B.php

but this:


/* file1.php */

$var = "foo";

function include_file($file_path)
{
require_once($_SERVER['DOCUMENT_ROOT'].$file_path);
}

include_file('/FOLDER1/FOLDER2/FOLDER3/file2.php');

/* file2.php */

include_file('/FOLDER1/FOLDER2/FOLDER3/FOLDER5/file3A.php');

include_file('/FOLDER1/FOLDER2/FOLDER4/file3B.php');

I get the following:

file1.php // $var is available and file2.php is included
file2.php // $var is not available; $GLOBALS['var'] is available; file3A.php and file3B.php are included
file3A.php // $var is not available; $GLOBALS['var'] is available;
file3B.php // $var is not available; $GLOBALS['var'] is available;

In the real application I'm not having access to the vars if not through $GLOBALS, is that because I must pass references to all set vars to the function?, if that's so, how could I go around this without that function?

I basically need to let files have access to any file in anyway without loosing references. Thanks for replying.

delinear
07-04-2005, 03:51 AM
Because $var is created outside the function and the page is called from within the function it makes sense that $var would only be available to the page via $GLOBALS, since it doesn't have scope within the function otherwise. There's a reference to this behaviour on the PHP site:

If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function.
Because include and require are language constructs and not functions, if you call the page using require_once directly instead of calling it via your function, it should inherit the normal scope of your variables:

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum