www.webdeveloper.com
Results 1 to 10 of 10

Thread: Referencing a file located in a different directory

  1. #1
    Join Date
    Mar 2004
    Location
    Nevada, USA
    Posts
    185

    Referencing a file located in a different directory

    I have the following directory structure in a website:

    main page
    --------folder called "includes"
    ------------file called "nav.inc"

    --------folder called "test"
    ------------file called index.shtml
    ----------------"Code from below"
    index.shtml

    I have tried to use the following code to load the nav.inc file into the file called index.shtml which is inside the folder called test, but none of these work.

    I get the error "an error occurred while processing this directive".


    <div id="navigation">
    <!--#include file="includes/nav.inc" -->
    </div>

    <div id="navigation">
    <!--#include file="/includes/nav.inc" -->
    </div>

    <div id="navigation">
    <!--#include file="./includes/nav.inc" -->
    </div>

    <div id="navigation">
    <!--#include file="../includes/nav.inc" -->
    </div>
    Last edited by lkeeney; 02-11-2014 at 06:12 PM. Reason: corrected code
    Larry

  2. #2
    Join Date
    Oct 2013
    Posts
    484
    Try:
    Code:
    <div id="navigation">
    <!--#include virtual="/includes/nav.inc" -->
    </div>
    Notice the lack of --> after </div> and "virtual" instead of "file".

  3. #3
    Join Date
    Mar 2004
    Location
    Nevada, USA
    Posts
    185
    Quote Originally Posted by Kevin2 View Post
    Try:
    Code:
    <div id="navigation">
    <!--#include virtual="/includes/nav.inc" -->
    </div>
    Notice the lack of --> after </div> and "virtual" instead of "file".
    I removed the --> which was a mistake in copying. I had originally used "virtual", but changed it to "file" when virtual didn't work.

    I tried it again with the --> removed, and with virtual and all the combinations of . ./ ../ and nothing works.
    Larry

  4. #4
    Join Date
    Oct 2013
    Posts
    484
    Quote Originally Posted by lkeeney View Post
    I tried it again with the --> removed, and with virtual and all the combinations of . ./ ../ and nothing works.
    /includes/nav.inc -------- no "dots", although from your folder structure you post above ../ should work.

    You could also try using a .txt file extension instead of .inc on your include.
    Last edited by Kevin2; 02-11-2014 at 06:58 PM.

  5. #5
    Join Date
    Mar 2004
    Location
    Nevada, USA
    Posts
    185
    Quote Originally Posted by Kevin2 View Post
    /includes/nav.inc -------- no "dots", although from your folder structure you post above ../ should work.

    You could also try using a .txt file extension instead of .inc on your include.
    Thank you again, buy I also tried it without the dots, and I did try .txt as well as .html.

    Nothing seems to work.

    I have been using this same type of structure on many different web sites and never had a problem before. I am sure I must be missing something simple. I thought by explaining my problem I might even see the problem myself.
    Larry

  6. #6
    Join Date
    Oct 2013
    Posts
    484
    Just noticed this. There's an extra space in the line after .inc". I'll underline it here:
    <!--#include file="/includes/nav.inc"_-->

    Maybe removing the space will help:
    <!--#include file="/includes/nav.inc"-->

    [edit]
    Or not... I tried it on one of my pages and no error either way. The only way to induce an error was if the server couldn't find the include file. So double-check the following:
    1) is the nav.inc file actually residing on the server?
    2) is nav.inc actually in the correct folder (includes)
    3) are nav.inc and includes spelled exactly as you reference them? Also no caps.
    Last edited by Kevin2; 02-12-2014 at 10:21 AM.

  7. #7
    Join Date
    Mar 2012
    Posts
    1,509
    Hi, SSI is new to me, so I looked up "#include virtual" in Wikipedia. If I've read it correctly, they suggest that the correct syntax is:

    <!--#include virtual="includes/nav.inc" -->

    i.e.
    - no space between "<!--" and "#include".
    - no "/" or "./" etc. before "includes".
    - 1 space before "-->".

    If you have tried that exact syntax, then perhaps:

    a) You have omitted the "s" in "index.shtml"? Or...

    b) Your server is not set up for SSI (or is set up in a non-standard way)? Or...

    c) Kevin2's comments in #6 apply?

    I hope this helps...

  8. #8
    Join Date
    Oct 2013
    Posts
    484
    Quote Originally Posted by jedaisoul View Post
    the correct syntax is:

    <!--#include virtual="includes/nav.inc" -->

    i.e.
    - no space between "<!--" and "#include".
    - no "/" or "./" etc. before "includes".
    - 1 space before "-->".
    The reason for the "leading slash" (/includes/nav.inc) is so one can use the same include line anywhere in a site structure. It basically says "go to root". If the leading slash isn't there, the server looks for the "includes" folder within the same folder as the referencing page. In the OP's case that referencing page is in a folder named "test", and the includes folder is not in "test".

    Quote Originally Posted by jedaisoul View Post
    a) You have omitted the "s" in "index.shtml"?
    OP claims that the "s" is there:
    --------folder called "test"
    ------------file called index.shtml
    And:
    I have tried to use the following code to load the nav.inc file into the file called index.shtml which is inside the folder called test
    But that is definitely something to double-check.

    Quote Originally Posted by jedaisoul View Post
    b) Your server is not set up for SSI (or is set up in a non-standard way)?
    Another thing to double-check. Some hosts do not allow SSI, but will allow PHP includes. A quick line or two in your .htaccess file and you can process HTML as PHP. Just as easy as SSI. Well, usually...

  9. #9
    Join Date
    Mar 2012
    Posts
    1,509
    Quote Originally Posted by Kevin2 View Post
    The reason for the "leading slash" (/includes/nav.inc) is so one can use the same include line anywhere in a site structure. It basically says "go to root". If the leading slash isn't there, the server looks for the "includes" folder within the same folder as the referencing page. In the OP's case that referencing page is in a folder named "test", and the includes folder is not in "test".
    As I said, I'm a novice in SSI, I use PHP. I agree that, in PHP "/" says "go to root", but it seems here that SSI uses "virtual" to mean that. Furthermore it says:

    "..."virtual" specifies the target relative to the domain root, while "file" specifies the path relative to the directory of the current file. When using "file" it is forbidden to reference to absolute paths".

  10. #10
    Join Date
    Oct 2013
    Posts
    484
    Interesting. I learned my one new thing for the day! I've always used "virtual" with the leading slash on my SSI sites. Never had a problem. Although I suppose "root" is "root" is "root".

    Point taken in regard to "file" though. Relative paths apply it seems. So one must use / or ../ or ../../ or whatever to get there.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles