Require_once() Problem - navigating file folders

I am having troubles with require_once() in my application.

So, I have two folders: ‘lib’ and ‘content’. Both are at the same level in the folder structure (i.e. ‘Application’ > ‘lib’ & ‘Application’ > ‘content’).

Under the ‘lib’ folder, I have a couple others:

  • lib > models
  • lib > views
  • lib > variables

Under my ‘content’ folder, I have the pages that are displayed in the browser (add.php, view.php).

On add.php, I have require_once(‘…/lib/views/addClass.php’);. When I run add.php in the browser, I get:

Warning: require_once(./variables/variables.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\application\lib\models\addClass.php on line 2

I can’t figure out why it can’t open the file. Any thoughts?

I am running XAMPP on Windows 7.

Relative paths are evil. I personally define the root path of my application as early as I can. That’s probably what’s going on here, check the path that PHP actually used to try to find the file and see if that’s correct.

Thanks for the response.

The funny thing is that I have a variables.php which I define my domain so that I can do something like require_once(DOMAIN . ‘/application/lib/views/add.php’); so I can avoid that but I have to pull in the variables.php file before I can use the constant.

How can I check the path that PHP used? Are you talking about “(include_path=‘.;C:\xampp\php\PEAR’)”?

You can carefully add the path

C:\xampp\htdocs\application\

… to that ini directive, then restart the webservice (Apache?)

However, if you do not have access to your ini file on your LIVE server then you can alter this directive using Apache instructions (in .htaccess say), and even at the page level if you have one single boot file that every public pages is handled by.

[fphp]ini_set[/fphp]

Otherwise you will have to adopt another practice such as having a setting in a config file which contains the actual include path.

There are other ways.

Hello.

Uhm, don’t you mean [fphp]set_include_path[/fphp]? I generally add a path like so:


<?php
set_include_path(
  __DIR__ . PATH_SEPARATOR . get_include_path( )
);

That works well for me.

Nope, works as I expect … what’s the benefit of set_include_path then?


<?php
$a = ini_get('include_path');
var_dump($a);

ini_set('include_path', 'esra_ym');

echo 'some stuff <hr /> ';

$b = ini_get('include_path');
var_dump($b);
?>
//string '.\\;\\var\\www\\includes;\\var\\www\\includes\\PEAR;' (length=77)
//some stuff <hr />
//string 'esra_ym' (length=7)

Hm, I guess there’s more than one way to skin a cat in this instance. I didn’t know your example actually would work. Another lesson learned :slight_smile: