Weird results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Weird results

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Weird results

Post 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);
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: Weird results

Post 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");
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: Weird results

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Weird results

Post 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";
Last edited by requinix on Mon Nov 03, 2008 4:33 pm, edited 1 time in total.
bmoyles0117
Forum Newbie
Posts: 22
Joined: Sun Nov 02, 2008 1:46 am

Re: Weird results

Post by bmoyles0117 »

be sure to make sure that the file permissions are readable.
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: Weird results

Post by Cirdan »

That makes sense. Thanks!
Post Reply