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.
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:
Alternatively you could try using the function set_include_path to modify your include path in your page that includes shownews.php.
MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
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?
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:
$path = str_replace('\\', '/', $path);
if($path == '/' || strpos($path, ':') !== false)
// Path is absolute.
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.
I'm not sure if this was any help, but I hope it didn't make you stupider.
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.