Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Sep 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    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.

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    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)

  • #4
    New to the CF scene
    Join Date
    Sep 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    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?

  • #6
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.

  • #7
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •