Professional Web Applications Themes

Relative paths and include files - PHP Development

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 ...

  1. #1

    Default 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.
    mark Guest

  2. #2

    Default Re: Relative paths and include files

    if you include all file to index.php you must using absolute path to access
    any file in your structure.

    for example if you put include("header.php") in index.php that's mean you
    writing the header.php code in index.php file. So if you want to access
    image1.gif , you must access it from images/image1.gif because you access it
    from index.php.

    p/s: corret me wrong

    "mark" <com> wrote in message
    news:dber1t$j9d$dmz.ncs.ea.ibs-infra.bt.com... 


    Fairul Guest

  3. #3

    Default Re: Relative paths and include files

    I store all my includes outside of the web in their own directory or
    directories. Where you have the index.php, I have an include file that is
    just a quickie config file setting the absolute path to the main include
    directory. Then I can use subdirectories in the web pages that pull the
    includes.

    Say I want to include this header:

    /www/somesite/php/includes/bluetemplate/header.inc.php

    in this index file:

    /www/somesite/htmldocs/index.php

    Then I would have this config file define the absolute path:

    /www/somesite/htmldocs/config.inc.php -> $inc_dir =
    "/www/somesite/php/includes/";

    And in index.php say this:

    require_once("config.inc.php");
    include_once( $inc_dir . "bluetemplate/header.inc.php");

    The way I work, I'd probably define a template variable in config.inc.php,
    too, because I'm so lazy:

    require_once("config.inc.php");
    include_once( $inc_dir . $template_name . "header.inc.php");

    Just make sure you are consistent with when you include slashed and don't
    and you are good to go.

    hth,
    John

    "mark" <com> wrote in message
    news:dber1t$j9d$dmz.ncs.ea.ibs-infra.bt.com... 
    /images/image1.gif? 


    Tex Guest

  4. #4

    Default Re: Relative paths and include files

    Thanks to Tex for posting. Your idea of putting php outside of the
    web directory, I think, is a god one, especially for security
    purposes.

    However I don't think your post helps with the original poster's
    problem of includes messing up the default directory.

    For example, a bit of html code in a page would work with <img
    src="images/test.prg or <img src="../images/test.prg, but when you
    put an include in hear or use urls like
    http://www.domain.com/filemane/variable/variable2

    the default or working directory gets very messed up and the two
    examples I have given for the img tag no longer work. Instead you
    have to change them to read <img
    src="http://www.domain.com/images/test.prg before they will work. Now
    I know you can substitute php variables in an do something like

    <img src="<? echo $WebURL; ?>mages/test.prg in order to fix this, but
    it would be better I think if yyou could issue a command in php to
    reset the workign directory or is it relative directory back to the
    state that my original img tags would work. I have tried the php
    command to change the directory to what it should be, but this does
    not help. Once I have "engaged" php, the whole concept of relative
    directory seems to go to heck.

    This is not a big problem, since I can fix it with the echo command as
    shown, but for testing purposes it is a real nuisance. I have to run
    everything off my web site and not off a local computer since I have
    to put the url into the html img tags.

    If you know a solution to this, I would love to know it.

    Again thanks for posting.
    Best regards,

    Al
    http://www.affordablefloridainsurance.com
    http://www.americanbestmortgages.com
    http://www.americanaffordablelifeinsurance.com
    AF Guest

Similar Threads

  1. relative include paths? What's the use?
    By Doug in forum PHP Development
    Replies: 8
    Last Post: January 29th, 09:57 AM
  2. #40666 [NEW]: handling of relative paths in include()
    By mfr at bmx-chemnitz dot de in forum PHP Bugs
    Replies: 0
    Last Post: February 28th, 12:39 PM
  3. Contribute 3 rewrites PHP include relative paths!
    By robcherny in forum Macromedia Contribute General Discussion
    Replies: 12
    Last Post: September 12th, 05:17 AM
  4. external linked files relative paths...how to?
    By nickelsock webforumsuser@macromedia.com in forum Macromedia Director Basics
    Replies: 0
    Last Post: August 2nd, 09:24 AM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139