Relative Paths and include files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mark

    Relative Paths and include files

    I am designing a website at the moment and looking at the difference
    between relative and absolute url links which is driving me crazy! I
    would like to use relative paths, but it is proving very restrictive as
    to how I design the file structure when it comes to including files. I
    currently have something like below:

    folder1
    folder2
    images
    ---image1.gif
    includes
    ---header.php
    ---footer.php
    folder3
    ---folder4
    ---file0.php
    ---file1.php
    ---file2.php
    index.php


    I am currently thinking I will have to run all my scripts at the same
    level to ensure I can include the header.php file in all my pages while
    maintaining the integrity of links in the header.php. For example, if I
    include header.php in index.php and header.php is referencing
    .../images/image1.gif in the images folder, the link will no longer be
    valid when included in index.php. Does that make sense?

    Can anybody share some tips on what they do to overcome what must be a
    very common problem? Is absolute paths the answer so instead of calling
    .../images/image.gif from the include file, I would call /images/image1.gif?

    Any help that anybody can give me would be much appreciated.

    Thanks,
    Mark.
  • Jerry Stuckle

    #2
    Re: Relative Paths and include files

    mark wrote:[color=blue]
    > I am designing a website at the moment and looking at the difference
    > between relative and absolute url links which is driving me crazy! I
    > would like to use relative paths, but it is proving very restrictive as
    > to how I design the file structure when it comes to including files. I
    > currently have something like below:
    >
    > folder1
    > folder2
    > images
    > ---image1.gif
    > includes
    > ---header.php
    > ---footer.php
    > folder3
    > ---folder4
    > ---file0.php
    > ---file1.php
    > ---file2.php
    > index.php
    >
    >
    > I am currently thinking I will have to run all my scripts at the same
    > level to ensure I can include the header.php file in all my pages while
    > maintaining the integrity of links in the header.php. For example, if I
    > include header.php in index.php and header.php is referencing
    > ../images/image1.gif in the images folder, the link will no longer be
    > valid when included in index.php. Does that make sense?
    >
    > Can anybody share some tips on what they do to overcome what must be a
    > very common problem? Is absolute paths the answer so instead of calling
    > ../images/image.gif from the include file, I would call /images/image1.gif?
    >
    > Any help that anybody can give me would be much appreciated.
    >
    > Thanks,
    > Mark.[/color]

    I generally use absolute pathnames. I seldom use relative ones.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • mark

      #3
      Re: Relative Paths and include files

      Thanks Jerry, the trouble I have is that my hosting provider sets the
      document root to /home so if I was to use absolute paths I would have to
      do somethign like

      /home/path/to/the/public_html/images/image1.gif

      which is a bit long winded. I could store it in a variable but it would
      be better if I could set document root to the public_html folder. Do you
      know how I can do this with out changing the httpd.conf file?

      Thanks.


      Jerry Stuckle wrote:[color=blue]
      > mark wrote:
      >[color=green]
      >> I am designing a website at the moment and looking at the difference
      >> between relative and absolute url links which is driving me crazy! I
      >> would like to use relative paths, but it is proving very restrictive
      >> as to how I design the file structure when it comes to including
      >> files. I currently have something like below:
      >>
      >> folder1
      >> folder2
      >> images
      >> ---image1.gif
      >> includes
      >> ---header.php
      >> ---footer.php
      >> folder3
      >> ---folder4
      >> ---file0.php
      >> ---file1.php
      >> ---file2.php
      >> index.php
      >>
      >>
      >> I am currently thinking I will have to run all my scripts at the same
      >> level to ensure I can include the header.php file in all my pages
      >> while maintaining the integrity of links in the header.php. For
      >> example, if I include header.php in index.php and header.php is
      >> referencing ../images/image1.gif in the images folder, the link will
      >> no longer be valid when included in index.php. Does that make sense?
      >>
      >> Can anybody share some tips on what they do to overcome what must be a
      >> very common problem? Is absolute paths the answer so instead of
      >> calling ../images/image.gif from the include file, I would call
      >> /images/image1.gif?
      >>
      >> Any help that anybody can give me would be much appreciated.
      >>
      >> Thanks,
      >> Mark.[/color]
      >
      >
      > I generally use absolute pathnames. I seldom use relative ones.
      >[/color]

      Comment

      • Jerry Stuckle

        #4
        Re: Relative Paths and include files

        mark wrote:[color=blue]
        > Thanks Jerry, the trouble I have is that my hosting provider sets the
        > document root to /home so if I was to use absolute paths I would have to
        > do somethign like
        >
        > /home/path/to/the/public_html/images/image1.gif
        >
        > which is a bit long winded. I could store it in a variable but it would
        > be better if I could set document root to the public_html folder. Do you
        > know how I can do this with out changing the httpd.conf file?
        >
        > Thanks.
        >
        >[/color]

        Mark,

        No, you can't change this without access to the httpd.conf file.

        However - I'd be VERY surprised if the document root is set to /home. This is
        the default directory for your web site; it should NOT be home.

        I suspect your real document root is something/public_html (or something
        similar). That's what's standard; if it isn't your entire web site would be broken.


        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • Geoff Muldoon

          #5
          Re: Relative Paths and include files

          mark@something. com says...
          [color=blue]
          > Can anybody share some tips on what they do to overcome what must be a
          > very common problem? Is absolute paths the answer so instead of calling
          > ../images/image.gif from the include file, I would call /images/image1.gif?[/color]

          My usual technique is to create a "config" file and populate it with a
          number of defined CONSTANTS (note: NOT variables) and then include it in
          each of the PHP files in my web application.

          Simplified example:

          <?php // config file
          define('WEB_ROO T', 'http://http_doc_root/');
          define('FILE_RO OT', '/absolute_php_ro ot/');
          define('IMAGE_W EB_PATH', WEB_ROOT.'image s/');
          define('IMAGE_F ILE_PATH', FILE_ROOT.'imag es/');
          define('INCLUDE _FILE_PATH', FILE_ROOT.'incl udes/');
          define('HEADER_ FILE', INCLUDE_FILE_PA TH.'header.php' );
          define('FOOTER_ FILE', INCLUDE_FILE_PA TH.'footer.php' );
          ?>

          <?php // sample app file
          //
          // first line below is only entry for this file where you might use a full
          // path but I usually use a relative one as I will know where this app
          // file is in the file system eg. maybe include '../includes/config.php'
          // instead
          //
          include 'includes/config.php';
          include HEADER_FILE;
          echo 'This is a picture: <br />';
          echo '<img src="'.IMAGE_WE B_PATH.'picture .jpg" alt="pic">
          include FOOTER_FILE;
          ?>

          Also excellent for portability within or across hosts, simply adjust in
          the config file only.

          Geoff M

          Comment

          • mark

            #6
            Re: Relative Paths and include files

            This is what my provider says:

            "
            There is no easy way to set the document root on the servers as we have
            them configured in a certain way which would mean making alot of
            changes. The servers use Apache mod_vhost_alias which is why the
            document root is /home You could use PHP or Perl to assign a value to a
            variable for your document root."

            Thanks for everyone's help. I'll check out all the suggestions and
            experiment...

            Mark.


            Jerry Stuckle wrote:[color=blue]
            > mark wrote:
            >[color=green]
            >> Thanks Jerry, the trouble I have is that my hosting provider sets the
            >> document root to /home so if I was to use absolute paths I would have
            >> to do somethign like
            >>
            >> /home/path/to/the/public_html/images/image1.gif
            >>
            >> which is a bit long winded. I could store it in a variable but it
            >> would be better if I could set document root to the public_html
            >> folder. Do you know how I can do this with out changing the httpd.conf
            >> file?
            >>
            >> Thanks.
            >>
            >>[/color]
            >
            > Mark,
            >
            > No, you can't change this without access to the httpd.conf file.
            >
            > However - I'd be VERY surprised if the document root is set to /home.
            > This is the default directory for your web site; it should NOT be home.
            >
            > I suspect your real document root is something/public_html (or something
            > similar). That's what's standard; if it isn't your entire web site
            > would be broken.
            >
            >[/color]

            Comment

            • Tony Marston

              #7
              Re: Relative Paths and include files


              "mark" <mark@something .com> wrote in message
              news:dbeu1l$p4$ 1@nwrdmz03.dmz. ncs.ea.ibs-infra.bt.com...[color=blue]
              > Thanks Jerry, the trouble I have is that my hosting provider sets the
              > document root to /home so if I was to use absolute paths I would have to
              > do somethign like
              >
              > /home/path/to/the/public_html/images/image1.gif
              >
              > which is a bit long winded. I could store it in a variable but it would be
              > better if I could set document root to the public_html folder. Do you know
              > how I can do this with out changing the httpd.conf file?[/color]

              You don't need to hard code the name of your root folder as it is available
              in $_SERVER['DOCUMENT_ROOT']. Check it out at


              --
              Tony Marston

              This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL




              Comment

              • Jerry Stuckle

                #8
                Re: Relative Paths and include files

                mark wrote:[color=blue]
                > This is what my provider says:
                >
                > "
                > There is no easy way to set the document root on the servers as we have
                > them configured in a certain way which would mean making alot of
                > changes. The servers use Apache mod_vhost_alias which is why the
                > document root is /home You could use PHP or Perl to assign a value to a
                > variable for your document root."
                >
                > Thanks for everyone's help. I'll check out all the suggestions and
                > experiment...
                >
                > Mark.
                >
                >
                > Jerry Stuckle wrote:
                >[color=green]
                >> mark wrote:
                >>[color=darkred]
                >>> Thanks Jerry, the trouble I have is that my hosting provider sets the
                >>> document root to /home so if I was to use absolute paths I would have
                >>> to do somethign like
                >>>
                >>> /home/path/to/the/public_html/images/image1.gif
                >>>
                >>> which is a bit long winded. I could store it in a variable but it
                >>> would be better if I could set document root to the public_html
                >>> folder. Do you know how I can do this with out changing the
                >>> httpd.conf file?
                >>>
                >>> Thanks.
                >>>
                >>>[/color]
                >>
                >> Mark,
                >>
                >> No, you can't change this without access to the httpd.conf file.
                >>
                >> However - I'd be VERY surprised if the document root is set to /home.
                >> This is the default directory for your web site; it should NOT be home.
                >>
                >> I suspect your real document root is something/public_html (or
                >> something similar). That's what's standard; if it isn't your entire
                >> web site would be broken.
                >>
                >>[/color][/color]

                If they told me that, I'd be looking for a new hosting company immediately (if
                not sooner).

                --
                =============== ===
                Remove the "x" from my email address
                Jerry Stuckle
                JDS Computer Training Corp.
                jstucklex@attgl obal.net
                =============== ===

                Comment

                Working...