help required using include()

Discussion in 'PHP' started by hawkoftheeye, Jun 4, 2008.

  1. #1
    Hi guys

    I have a small problem that im hoping someone here can help me with.

    I am developing a photo website. I have configured the directory structure as such so that each photoset lives at /public_html/photosets/setname/
    I have various scripts that I call from the index.php page, each of these scripts lives at public_html/inc/
    My problem is that when I try to include the file, like so:

    include('\inc\script.php');
    
    Code (markup):
    It will not work, saying it cannot find the file in question.

    I have tried various other combinations, including referring to the entire URL, but I cannot seem to get it to work.
    I even considered moving the scripts to the same folder as the calling page, but as there are many photosets, all currently living within their own directory it would be practical and difficult to maintain.
    I get the feeling im missing something painfully obvious...

    Can anybody help me work this out? Theres a beer in it if anyone can (but you'll have to come here as I can't drive :) )

    Many thanks

    Richard
     
    hawkoftheeye, Jun 4, 2008 IP
  2. Denn

    Denn Peon

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You tired just to write:
    include("inc/script.php");
    ?
     
    Denn, Jun 4, 2008 IP
  3. hawkoftheeye

    hawkoftheeye Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I have. In fact, ive tried both of these:

    include('inc/script.php');
    include('inc\script.php');
    Code (markup):
    Neither of which seem to work.
     
    hawkoftheeye, Jun 4, 2008 IP
  4. X.Homer.X

    X.Homer.X Peon

    Messages:
    290
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    as far as i know include only works when the file being included is in the same directory as the file that is calling the function.
     
    X.Homer.X, Jun 4, 2008 IP
  5. Denn

    Denn Peon

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    To get you right, your structure of your webpage is
    /public_html/index.php
    /public_html/inc/script.php
    /public_html/photoset/
    /public_html/photoset/set01/


    And you are trying to include script.php from index.php?

    X.Homer.X - you are being completely wrong
     
    Denn, Jun 4, 2008 IP
  6. hawkoftheeye

    hawkoftheeye Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It would certainly seem that way. Surely there must be some way to include a script that is elsewhere on the server?
    I don't want to have to include the script in each individual folder, as this would be a nightmare to maintain, nor do I really want to have all the photosets in the same folder.
     
    hawkoftheeye, Jun 4, 2008 IP
  7. X.Homer.X

    X.Homer.X Peon

    Messages:
    290
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    yeah this is something that i have come across also, i asked around and noone has an answer to how to incloude a script from a parent or sibling folder.

    well thats what i was told, im sorry if i dont know every fucking thing about php, i said AS FAR AS I KNOW
     
    X.Homer.X, Jun 4, 2008 IP
  8. Denn

    Denn Peon

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I do PHP programmering everyday - and you are able to include files from a different directory - even another server.
     
    Denn, Jun 4, 2008 IP
  9. hawkoftheeye

    hawkoftheeye Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    @ Denn:

    The page the include is executed from is:

    /public_html/photosets/setname/index.php

    The location of the script is:

    /public_html/inc/script.php

    And yes, I am trying to include the script from index.php

    Thanks
     
    hawkoftheeye, Jun 4, 2008 IP
  10. X.Homer.X

    X.Homer.X Peon

    Messages:
    290
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #10
    oh really programmering, thats not an actual word :p you must mean programming
     
    X.Homer.X, Jun 4, 2008 IP
  11. Denn

    Denn Peon

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    hawkoftheeye: Then I got your problem ;)
    try include("../../inc/script.php");

    X.Homer.X - English is my second language :)
     
    Denn, Jun 4, 2008 IP
  12. X.Homer.X

    X.Homer.X Peon

    Messages:
    290
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #12
    lol oh sorry, im just so used to everyone knowing english as their first language and speaking it stupidly.

    also, i never thought that ../../ will would work
     
    X.Homer.X, Jun 4, 2008 IP
  13. hawkoftheeye

    hawkoftheeye Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Works like a charm :)

    Many thanks for all your help guys :) And the beers yours Denn - you'll have to come and get it though ;)
     
    hawkoftheeye, Jun 4, 2008 IP
  14. Denn

    Denn Peon

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    No problem :)
    X.Homer.X - we learn everyday, don't we? :)
     
    Denn, Jun 4, 2008 IP
  15. X.Homer.X

    X.Homer.X Peon

    Messages:
    290
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #15
    this does not work with me. this is my folder layout.

    /public_html/index.php
    /public_html/includes/script1.php
    /public_html/includes/script2.php
    /public_html/admin/index.php

    im trying to include script2.php from script1.php and including script2.php from admin/index.php do you understand? here

    script1.php
    
    <?php
    include('script2.php');
    ?>
    
    Code (markup):
    admin/index.php
    
    <?php
    include('../includes/script1.php');
    ?>
    
    Code (markup):
    thanks.
     
    X.Homer.X, Jun 4, 2008 IP
  16. alecs

    alecs Well-Known Member

    Messages:
    156
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    128
    #16
    The error should tell you exactly where it didn't find the specified file.
    BTW, there's some weird stuff with cases like yours when C.php is included in B.php which is included by A.php. More about that on http://www.php.net/manual/en/function.include.php :
     
    alecs, Jun 4, 2008 IP
  17. X.Homer.X

    X.Homer.X Peon

    Messages:
    290
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #17
    im including my top.php file, and thats working, but in top.php im including config.php, and my css is declared in top.php

    config and my css are not working, its looking for config in includes/config.php (where it is) but is that looking in the directory of the file being showed? or the directory of the page being included.

    do you get what im saying, on my admin/ directory my index is calling


    home/admin/index.php
    
    <?php
    require("../includes/top.php");
    require("includes/content.php");
    require("../includes/bottom.php");
    ?>
    
    PHP:

    home/includes/top.php
    
    <?php
    ob_start();
    include('config.php');
    ...
    check if admin
    redirect to main page
    ...
    ?>
    <head>
    <link rel=/css.css></link> <!--whatever this code is. css is in home/ directory-->
    </head>
    <body>
    <header>
    <nav>
    </body>
    
    PHP:
    How do i make it so the css is included and the config is included?
     
    X.Homer.X, Jun 4, 2008 IP
  18. alecs

    alecs Well-Known Member

    Messages:
    156
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    128
    #18
    The CSS path is interpreted by the browser. So the path in the href is relative to the path showing in the address bar. Try to make it relative to the path of the original .php file (not some included file).
     
    alecs, Jun 4, 2008 IP
  19. X.Homer.X

    X.Homer.X Peon

    Messages:
    290
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #19
    what are you talking about. my css file is a .css file not a .php file. it is included using the link rel=stylesheet thing config is being included with .php include
     
    X.Homer.X, Jun 4, 2008 IP
  20. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #20
    Make it relative to the root of the web server for the CSS file. In english, make it work from /.
    So if you have your CSS file in /css , the path would be /css/style.css. If its at the top just put /style.css
    But:
    <link rel=/css.css></link>
    That is problematic, should be link rel="stylesheet" href="/css.css" type="text/css" . Far as config.php not being included, I would have to recommend putting it as

    include ("./config.php");
    if that doesn't work, *shrug*
     
    shallowink, Jun 4, 2008 IP