Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-02-2007, 07:04 AM   PM User | #1
Viral
New to the CF scene

 
Join Date: Sep 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Viral is an unknown quantity at this point
Including an include

Hello!

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.

Thanks.
Viral is offline   Reply With Quote
Old 01-02-2007, 10:10 AM   PM User | #2
koyama
Senior Coder

 
koyama's Avatar
 
Join Date: Dec 2006
Location: Copenhagen, Denmark
Posts: 1,246
Thanks: 1
Thanked 5 Times in 5 Posts
koyama will become famous soon enough
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:
PHP Code:
include $_SERVER['DOCUMENT_ROOT'].'/include/mysql.php'
Alternatively you could try using the function set_include_path to modify your include path in your page that includes shownews.php.
koyama is offline   Reply With Quote
Old 01-02-2007, 12:56 PM   PM User | #3
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,904
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
I agree, absolute paths solve many issues (including yours).
__________________
resistance is...

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)
firepages is offline   Reply With Quote
Old 01-02-2007, 08:22 PM   PM User | #4
Viral
New to the CF scene

 
Join Date: Sep 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Viral is an unknown quantity at this point
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.
Viral is offline   Reply With Quote
Old 01-02-2007, 11:19 PM   PM User | #5
koyama
Senior Coder

 
koyama's Avatar
 
Join Date: Dec 2006
Location: Copenhagen, Denmark
Posts: 1,246
Thanks: 1
Thanked 5 Times in 5 Posts
koyama will become famous soon enough
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?
koyama is offline   Reply With Quote
Old 01-03-2007, 12:01 AM   PM User | #6
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
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.
PHP Code:
<?php
$root 
'..';
include 
$root '/dir/file.ext';
?>
include_rel could look something like this:
PHP Code:
function include_rel($path)
{
    
$path str_replace('\\''/'$path);

    if(
$path[0] == '/' || strpos($path':') !== false)
    {
        
// Path is absolute.
        
include $path;
        return;
    }
    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.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 01-03-2007, 01:03 AM   PM User | #7
koyama
Senior Coder

 
koyama's Avatar
 
Join Date: Dec 2006
Location: Copenhagen, Denmark
Posts: 1,246
Thanks: 1
Thanked 5 Times in 5 Posts
koyama will become famous soon enough
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.
koyama is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:43 PM.


Advertisement
Log in to turn off these ads.