SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Quick question about file locations

    Hey,

    I have my main index.php file and in there I include a top.php and a bottom.php file which are stored in an includes folder.

    As follows:

    PHP Code:
    <?php require_once("includes/top.php"); ?>
    and

    PHP Code:
    <?php require_once("includes/bottom.php"); ?>
    Now, I have added an admin folder which will store all the documents related to the sites cms, I built a page of the cms as index.php but within the admin folder and also used includes to add the top and bottom pages but included them as:

    PHP Code:
    <?php require_once("../includes/top.php"); ?>
    Basically just adding "../" to the file path so it can find the files.

    However...

    In the top.php file I include a file named db.php also stored in the includes file like:

    PHP Code:
    <?php require_once("includes/db.php"); ?>
    Now, when I view my main cms page, the top and bottom files are includes fine but those files cannot find the db.php page as that icnlude link doesn;t have the "../" in front of the pathname.

    How can I get the reference to work from both the CMS folder and the main website files.

    I did expect it to work but it doesn't.

    I would appreciate quick help with this if possible.

    Thanks

    Neil

  2. #2
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why don't you try to take a look at the include path: PHP: Description of core php.ini directives - Manual , and the set_include_path() function?

  3. #3
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm confused.

    I'm only usingt he standard include() function. No special folders, the includes folder I ahve is one I created and is stored in a project folder under my web root.

    I just can't see how I can get the include file to find db.php from within the admin cms system and from when top.php is included in the main site outside of the admin folder.

    Thanks

    Neil

    EDIT: So would this work:

    PHP Code:
    set_include_path('../');
    require_once(
    "includes/top.php"); 

  4. #4
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From your original post it seems to me that you have top.php and db.php in the same dir, right? If that is so, have you tried to include db.php from top.php simply as
    PHP Code:
    include('db.php'
    , without any path? I think it should work, as they are in the same dir.

  5. #5
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that works. Thanks!

  6. #6
    SitePoint Member
    Join Date
    Mar 2011
    Location
    Tyne & Wear
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using absolute paths is a good approach too, there are many ways to do this depending on how you have built your web application.

    Systems such as APC cache dont tend to like relative paths much.

    Simple example (Apache Only)

    $docRoot = getenv("DOCUMENT_ROOT");
    include $docRoot."/includes/top.php";

    This will ensure you are always loading the file from where you expect, without the relative path nightmare.

  7. #7
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    I have just realised when I went back to work on the main website part of this site that, I get an error as a file I have included is not being located correctly.

    Just a quick question here about absolute paths:

    By the way, I am currently running this sites on my local WAMP server so it's all offline at the moment.

    I ahve my webroot which is a folder called "www". Then for each website or application I build, I add a subfolder of "www" with the name of that site.

    I know I can get the document root by using the following:

    $docRoot = $_SERVER['DOCUMENT_ROOT'];

    I could do that but to make the path correct I would have to do:

    $docRoot = $_SERVER['DOCUMENT_ROOT'].'\projectfoldernamehere'\;

    If I added at the top of each of my pages to amke sure all the includes were correct then if I were to change the name of the project folder in the future, I would ahev to go and change all references to that folder in EVERY page of code.

    Is there a way for it to find the project folder name automatically?

    Thanks

    Neil

  8. #8
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any help from anyone?

  9. #9
    SitePoint Member
    Join Date
    Mar 2011
    Location
    Tyne & Wear
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would have to see how your files are set out, to see where you are coming from.

    We use our own framework so all execution runs through the main script file triggered by index.php, so this makes it very easy to handle paths.

  10. #10
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here:

    PHP Code:
    www project 1 index.php
                           
    includes top.php
                                         
    bottom.php
                                         
    db.php
                           
    css style.css
            
    project 2 index.php
                           
    includes top.php
                                         
    bottom.php
                                         
    db.php
                           
    css style.css
            
    project 3 index.php
                           
    includes top.php
                                         
    bottom.php
                                         
    db.php
                           
    css style.css 
    I know it's not code but that keeps the white space

    That's an example of my folder structure

  11. #11
    SitePoint Member
    Join Date
    Mar 2011
    Location
    Tyne & Wear
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your calling scripts are all in the root folder you could try this method.


    index.php (or any script)

    define('PROJECT_ROOT', dirname(__FILE__));

    include PROJECT_ROOT.'/includes/top.php';
    include PROJECT_ROOT.'/includes/db.php';
    // Do your jingle here

    include PROJECT_ROOT.'/includes/bottom.php';

    This will take the parent folder and define a global const for it.

    Hope this helps.

  12. #12
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but would I ahve to redefine that on my other pages as well as on index.php?

    Another problem is that I sometimes have an admin folder in the project folder which I basically run as a separate site but it still includes both the top and bottom php include files from outside of the admin folder. How would this work for those? as this was what this topic was originally about?

  13. #13
    SitePoint Member
    Join Date
    Mar 2011
    Location
    Tyne & Wear
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have the subfolder admin in your project you can simply do the following

    PHP Code:
    //admin.php

    define('PROJECT_ROOT'dirname(dirname(__FILE__));

    include 
    PROJECT_ROOT.'/includes/top.php';
    include 
    PROJECT_ROOT.'/includes/db.php';
    // Do your jingle here

    include PROJECT_ROOT.'/includes/bottom.php'

  14. #14
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right, I'll give it a go, it does seem an overly long way to go for something like this.

    Basically, how does everyone else deal with this issue?

    Another problem I ahve discovered is that, my dbd.php page is included in my top.php page whcih is itself included at the top of all my site pages. I included db.php in top.php as to me that's the most logical place for it to be included.

    now, the top.php pahe is being included in site pages stored in the project root folder AND ALSO in site pages in the admin folder stored within the project root folder. If I get db.php including correctly from the main sites perspective, as soon as you open a page from the admin areas perspective, it has an error as it cannot be located!!!!!

  15. #15
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right, As I can't seem to figure out another solution that will work I am settling with doing the following:


    Specifying the $projectRoot variable at the top of each of my main pages:
    PHP Code:
    $projectRoot dirname(__FILE__); 
    and doing any includes like:
    PHP Code:
    require_once($projectRoot.'/includes/top.php'); 
    and specifying the $projectRoot from inside other subfolders as:
    PHP Code:
    $projectRoot dirname(dirname(__FILE__)); 

    Main downside is that I have to remember to put in that $projectRoot variable at the top of every page of my site.

    Thanks

    Neil


Bookmarks

Posting Permissions

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