View Full Version : Including an include

01-02-2007, 08:04 AM

I need help. I'm building a PHP News system, and I have a file named "shownews.php."

shownews.php does lots of things, but it uses mySQL, therefore it includes ANOTHER file named mysql.php.


Now, I want to INCLUDE the shownews.php on my home page. I do so, but then I get lots of errors, because PHP couldn't find mysql.php, because it tried to locate the file from the home page's perepecive, and not shownews.php. Therefore, the whole thing is blown, and nothing works.

Can you guys help? I NEED shownews.php to include the file, because lots of other pages use mysql.php.


01-02-2007, 11:10 AM
I don't hope I'm giving you bad advice, but I think your best bet is to try to edit shownews.php, find the line where mysql.php is included, and change to absolute path. Let's say you have in your web root: inlcude/mysql.php. You would then include it in shownews.php using:

include $_SERVER['DOCUMENT_ROOT'].'/include/mysql.php';

Alternatively you could try using the function set_include_path (http://php.net/manual/en/function.set-include-path.php) to modify your include path in your page that includes shownews.php.

01-02-2007, 01:56 PM
I agree, absolute paths solve many issues (including yours).

01-02-2007, 09:22 PM
There is one problem.

If this app is going to be distributed to lots of people, people may change the dir ectory the install is it.

So I can't be sure where to start the include path if I don't know the directory the installation is stored in.

01-03-2007, 12:19 AM
I see what you mean, but I'm affraid I must have to give up on this one.

The way that php handles nested includes has caused me plenty of problems. From what I can see by searching, others have been suffering too. Eventually each coder seems to find his own workaround to fix the problem--all being more or less ugly.

In short, php's way of handling includes doesn't seem to correspond to the actual way we build applications. We start with tiny scripts without knowing exactly where they will end up as the project grows.

Since I have limited experience, I wouldn't be criticizing php, but I sure hope there is a reason why I must have so much head ache.

To firepages and other php gurus: why don't we have something like include_rel that would include relative to the script which the statement is written in?

01-03-2007, 01:01 AM
You could just add a variable $root (or a constant), that will hold the path to the root of your project and simply prefix all the paths with it.

$root = '..';
include $root . '/dir/file.ext';

include_rel could look something like this:

function include_rel($path)
$path = str_replace('\\', '/', $path);

if($path[0] == '/' || strpos($path, ':') !== false)
// Path is absolute.
include $path;
include str_replace('\\', '/', pathinfo(__FILE__, PATHINFO_DIRNAME)) . '/' . $path;
// The str_replace makes it pretty. It could be removed.
// Backsalshes in __FILE__ are given only on windows, which allows both types.

Though the original include is a language construct and not a function so you will need the () around the argument.

01-03-2007, 02:03 AM
Fantastic marek_mar this is so far the best I've seen... what you are saying is that __FILE__ is evaluated with respect to the script it originally belonged in and not with respect to the executed script it is included in. Thanks a lot.

Viral: I'd definitely stick to marek_mar.