Page 1 of 1

Require_once fails when entering parent directory

Posted: Sun Dec 12, 2010 4:29 pm
by tom.pauwaert
Hi!

I have been googling for hours on this problem I'm having, and I can't seem to see anything i'm doing wrong at all.
All the examples seem to match what I'm doing and still I'm getting errors. I'm out of ideas on what to do.

The problem is with inclusion, or rather, require_once.
I have this directory structure with a number of files in it:
[text]
Buddylink (dir)
| buddylinker.administer.subjects_e.inc (file)
-- administration (dir)
-- | SchoolAdminFormCreator.php (file)
-- shared (dir)
-- | visitor.php (file)
-- | Institutions.php (file)

Where '|' means 'in the aforementioned directory'
[/text]

So, the thing is, from the "subjects_e.inc" file i can perfectly include either visitor.php or SchoolAdminFormCreator.php by using

Code: Select all

require_once "administration/SchoolAdminFormCreator.php";
require_once "shared/visitor.php";
If I try to require for example Institutions.php from the 'visitor.php' file, there's no problem there:

Code: Select all

require_once "Institutions.php";
That works brilliantly.

Now, the issue is when I try to include for example 'visitor.php' from the 'SchoolAdminFormCreator.php', i tried this among many other things:

Code: Select all

require_once "../shared/visitor.php";
that, unfortunately, does not work for a very obscure reason. The error is:
[text]Fatal error: require_once() [function.require]: Failed opening required '../shared/visitor.php' (include_path='.;C:\php5\pear') in C:\wamp\www\sites\all\modules\buddylinker\administration\SchoolAdminFormCreator.php on line 10[/text]

The same problem arises when I try to include any file at all in a parent directory, from, whichever directory I try it from.
For example - from visitor.php:

Code: Select all

require_once "../buddylinker.administer.subjects_e.inc";
Could anyone please enlighten me on which incredibly dumb thing I'm doing wrong? :)
I'm totally clueless now.

Thanks a lot in advance!!

-Tom

Re: Require_once fails when entering parent directory

Posted: Sun Dec 12, 2010 5:16 pm
by walkblind
that could be a permissions issue. check your directory and file permissions to make sure they have the proper read, write, execute settings. Also, try using the absolute path name. You're using a windows server so something like c:\your\path\to\file.php. You can read the require_once manual at php.net http://php.net/manual/en/function.require-once.php. There are loads of helpful comments regarding this function and its quirks. Hope this helps. :D

Re: Require_once fails when entering parent directory

Posted: Mon Dec 13, 2010 6:31 am
by tom.pauwaert
Hm, the thing is that all of this is developed locally where I shouldn't be having any permission issues at all. And permission being okay to read/write/execute in a parent and child directory, but not being able to access the same parent directory from the child directory would seem kind of odd. But i'm very sure it's not a permission issue really :S.

I'll try using the absolute path name next. And check the manual, however I believe I had checked it already and not found anything to describe this issue. It's so weird and brainwrecking that I feel like hitting my head on a block of concrete with nails sticking out of it :S

Thanks for the reply though ;)

x

Re: Require_once fails when entering parent directory

Posted: Mon Dec 13, 2010 11:03 am
by AbraCadaver
Paths are not always relative to the file that is including them, all paths are relative to the file that is loaded first, so which file is loaded in the browser?

Re: Require_once fails when entering parent directory

Posted: Tue Dec 14, 2010 5:39 am
by tom.pauwaert
Thanks a lot for the advice. I got it to work now, with a slight workaround so to say.
I wouldn't exactly call it the appropriate solution, but at least I can continue with my work now. For your interest, if you care to challenge yourself with the weird quircks of php, I would very much like to know the explanation for this behaviour if anyone can give it :).

Well, since I'm building a drupal module. I guess the file that is loaded first would be the .module file. However, I've tried changing the path to just about all the different possible directories that could be loading it.

Code: Select all

require_once 'shared/visitor.php';  // if it calls from the top directory which has the '.module' file and another file which require_once's some files in subfolders and works correctly.
require_once '../shared/visitor.php'; // if it calls from the directory that has the file that includes the visitor.php file.
require_once 'visitor.php'; // just to check for weird quircks, alas...
So, none of that worked, however, we then tried realpath('.'); to see which directory the file was relative to.
This for some really odd reason seemed to be my webservers root directory. Or in my wamp installation, that was the /wamp/www.

Anyways. Working around by including like this, seems to work:

Code: Select all

require_once('./sites/all/modules/buddylinker/shared/visitor.php');
However, I then seriously start questioning why my other includes seem to be working perfectly by just using the relative directory to the file that is including them. Anybody have any insights on this?
If you want to know the whole include hierarchy so to say, or at least from my module, it is the following:
[text]
(Hierarchy goes down, top file includes file below)
buddylinker.module
--| buddylinker.administration.schoolsdep.inc
--|--|shared/institutions.php
--|--|--|factory.php
--|--|administration/SchoolAdminFormCreator.php
--|--|--|../shared/visitor.php (but the require_once function has to use the following path: ./sites/all/modules/buddylinker/shared/visitor.php)
[/text]

So, except at the visitor.php file, all the include files I put here are the actual paths I'm using in the require_once function. And they all work perfectly, except that one pain in the ass visitor.php that does not want to be included from a relative directory, or rather, that is run from my webserver root directory, for some, in my eyes, totally inexplicable reason.

Thanks in advance!
-Tom

Re: Require_once fails when entering parent directory

Posted: Tue Dec 14, 2010 10:42 am
by AbraCadaver
So some file that is loaded first by drupal (the file in the browser address bar), probably index.php is located in /wamp/www/ so all files are relative to that no matter where your other files are located.

So if you want to include a file in the root dir (/wamp/www/something.php) you would just do:

Code: Select all

require('something.php');
Regardless of where the including file is located.

Also, to include lower directories you would do:

Code: Select all

require('shared/something.php');
Regardless of where the including file is located.

Re: Require_once fails when entering parent directory

Posted: Tue Dec 14, 2010 12:13 pm
by tom.pauwaert
Yeah, that's understandable. But it doesn't make sense why the other includes work perfectly fine, and just this one does not. Because they all use relative paths. And the only require_once function complaining is the one that needs the parent dir :S