Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-01-2005, 10:29 AM   PM User | #1
maltrecho
Regular Coder

 
Join Date: Feb 2003
Posts: 345
Thanks: 0
Thanked 0 Times in 0 Posts
maltrecho is an unknown quantity at this point
Arrow includes and relative paths

Having the following, why file3A.php is being included while file3B.php is not?:
Code:
+ FOLDER1
  - file1.php
  + FOLDER2
    + FOLDER3
      - file2.php
      + FOLDER5
        - file3A.php
    + FOLDER4
      -file3B.php
PHP Code:
/* file1.php: */

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

/* file2.php: */

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

require_once('../FOLDER4/file3B.php'); //worng 
maltrecho is offline   Reply With Quote
Old 07-01-2005, 12:50 PM   PM User | #2
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
../ Means back a directory. I have no idea where abouts you are in that hierarchy.
__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks
Nightfire is offline   Reply With Quote
Old 07-01-2005, 01:01 PM   PM User | #3
maltrecho
Regular Coder

 
Join Date: Feb 2003
Posts: 345
Thanks: 0
Thanked 0 Times in 0 Posts
maltrecho is an unknown quantity at this point
Quote:
Originally Posted by Nightfire
../ 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 is offline   Reply With Quote
Old 07-01-2005, 05:46 PM   PM User | #4
maltrecho
Regular Coder

 
Join Date: Feb 2003
Posts: 345
Thanks: 0
Thanked 0 Times in 0 Posts
maltrecho is an unknown quantity at this point
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. [...]
maltrecho is offline   Reply With Quote
Old 07-01-2005, 10:41 PM   PM User | #5
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
You could play a bit with the realpath() function.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 07-02-2005, 12:59 PM   PM User | #6
maltrecho
Regular Coder

 
Join Date: Feb 2003
Posts: 345
Thanks: 0
Thanked 0 Times in 0 Posts
maltrecho is an unknown quantity at this point
Having the same hierarchy:
Code:
+ FOLDER1
  - file1.php
  + FOLDER2
    + FOLDER3
      - file2.php
      + FOLDER5
        - file3A.php
    + FOLDER4
      -file3B.php
but this:
PHP Code:
/* 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.

Last edited by maltrecho; 07-02-2005 at 01:02 PM..
maltrecho is offline   Reply With Quote
Old 07-04-2005, 03:51 AM   PM User | #7
delinear
Regular Coder

 
Join Date: Feb 2005
Location: West Midlands, UK
Posts: 623
Thanks: 0
Thanked 0 Times in 0 Posts
delinear is an unknown quantity at this point
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:
Quote:
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:
Quote:
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.
__________________
~ Bazzy
delinear is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:51 AM.


Advertisement
Log in to turn off these ads.