Page 1 of 1

Weird results

Posted: Mon Nov 03, 2008 2:37 pm
by Cirdan
I have a script in a directory directory below the http root directory.

./www/ - document root, index.php
./project/ - scripts

I am trying to include a file in the directory, and execute code inside that included file. What is happening is, after the include() or require_once() statement, nothing is being executed. If i put an echo "hello"; after the include statement, "hello" does not get printed out to the page. Is there a setting I need to change to allow scripts outside the document root directory? I tried putting this at the beginning of of the file: ERROR_REPORTING(E_ALL); but no errors are being printed on the screen at all.

Re: Weird results

Posted: Mon Nov 03, 2008 2:47 pm
by requinix
A blank page typically means a syntax/parse error. Check that included file for typos, missing braces, and the like.
Probably not the case, but remember that calling exit/die inside an included file kills off the entire script, not just the current file.

Whether errors are displayed is also controlled by the display_errors INI setting. To turn it on temporarily:

Code: Select all

ini_set("display_errors", 1);

Re: Weird results

Posted: Mon Nov 03, 2008 3:07 pm
by Cirdan
EDIT: Nevermind, I got it to work. Thanks!

Okay, I got PHP to print errors, it can't find the file I tried to include. If I try to require or require_once it gives me a "unexpected T_VARIABLE, expecting T_FUNCTION" error.

With my structure like this:

http/www - root directory, index.php
http/project/providers/tests/

...is this correct from index.php?

include("../project/providers/tests/TestFrontController.php");

Re: Weird results

Posted: Mon Nov 03, 2008 3:34 pm
by Cirdan
Bah, this is ridiculous and is making me frustrated. I checked the paths and file names multiple times.

Image

inside:
project/providers/tests/TestFrontController.php
I need to include:
project/foundation/simpletest/autorun.php

require_once('../../foundation/simpletest/autorun.php');

But it says it can't find the file. This is getting annoying. Can require_once not travel that much in the directory structure or something?

Re: Weird results

Posted: Mon Nov 03, 2008 4:24 pm
by requinix
include and require paths are relative to the current working directory, which is the directory where the initial file is.
So if /a.php includes /a/b.php which includes /a/b/c.php, and c.php tries to include "../path/file.php" then it's relative to / and not to /a/b/.

Which is why it's better to use absolute paths.
$_SERVER[DOCUMENT_ROOT] points to the web root (your www folder) when the web server runs it. So to include something under /project it would be

Code: Select all

require_once $_SERVER["DOCUMENT_ROOT"] . "/../project/foundation/simpletest/autorun.php";
Thing is, that file is located outside the web root, so it's not guaranteed that $_SERVER[DOCUMENT_ROOT] is available. And that's a pain to maintain if you ever move stuff.
So you can use a combination of dirname() and __FILE__ instead:

Code: Select all

require_once dirname(__FILE__) . "/../../foundation/simpletest/autorun.php";

Re: Weird results

Posted: Mon Nov 03, 2008 4:27 pm
by bmoyles0117
be sure to make sure that the file permissions are readable.

Re: Weird results

Posted: Mon Nov 03, 2008 4:36 pm
by Cirdan
That makes sense. Thanks!