Professional Web Applications Themes

Relative path & php - PHP Development

Help. Every time I use php my relative path gets messed up. For example take this code in an html file: ( I have abbreviated it for illustration purposes) <img src="library_files/logo.gif" alt="Mortgage Loans" border="0" width="128" height="122"></a><br><br></td> <a href="index.html"><font size="1">Home</font></a></p> works great. I then put it into any php file and it doesn't. I have to change it to this in order to get it to work. <img src="http://www.domainname.com/library_files/logo.gif" alt="Mortgage Loans" border="0" width="128" height="122"></a><br><br></td> <a href="http://www.domainname.com/index.html"><font size="1">Home</font></a></p> Does anyone know why this is happening and how to fix the problem? When I say fix, I mean set the path programmatically so ...

  1. #1

    Default Relative path & php

    Help.

    Every time I use php my relative path gets messed up.

    For example take this code in an html file: (
    I have abbreviated it for illustration purposes)

    <img src="library_files/logo.gif" alt="Mortgage Loans" border="0"
    width="128" height="122"></a><br><br></td>
    <a href="index.html"><font size="1">Home</font></a></p>

    works great.

    I then put it into any php file and it doesn't.

    I have to change it to this in order to get it to work.

    <img src="http://www.domainname.com/library_files/logo.gif"
    alt="Mortgage Loans" border="0" width="128"
    height="122"></a><br><br></td>
    <a href="http://www.domainname.com/index.html"><font
    size="1">Home</font></a></p>

    Does anyone know why this is happening and how to fix the problem?

    When I say fix, I mean set the path programmatically so that I do not
    have to go manually edit every file and add http://www.domainname.com/
    to every href and src tag.

    Thanks for any help.
    AF Guest

  2. #2

    Default Re: Relative path & php

    AF wrote: 

    Try prefixing with './' - as in:

    <img src="./library_files/logo.gif" alt="Mortgage Loans" border="0"
    width="128" height="122"></a><br><br></td>
    <a href="index.html">
    Derick Guest

  3. #3

    Default Re: Relative path & php

    AF wrote: 


    It's probably not caused by PHP but by the way you include files.
    Assume we have:

    PHP file "top.inc" located in root folder containing:
    <div>
    This is top of page with some image: <img src="some_image.gif" />
    </div>

    File "some_image.gif" is also located in root folder.
    In the same (root) folder we have also "index.php" file containing:
    <html>
    <body>
    <?php
    include( 'top.inc' );
    ?>
    <div>
    This is a main page.
    </div>
    </body>
    </html>

    This file will display the image properly. But if we also have
    subfolder called "sub" containing "index.php" file with:
    <html>
    <body>
    <?php
    include( '../top.inc' );
    ?>
    <div>
    This is a subpage.
    </div>
    </body>
    </html>

    Then the "sub/index.php" will not display the image properly
    cause image tag contains relative path to "some_image.gif"
    which is now relative to "sub" folder (from which we called
    "index.php"). The fact that "top.inc" file is in root folder
    is irrelevant here.

    You'll have similar problem if the "top.inc" and "some_image.gif"
    were located in "sub" folder but included in PHP file called
    from root folder.

    When you use relative paths in PHP, you have to consider
    where your files are located and where from they are included.
    All relative URLs (<a href>, <img src>, <script src> etc.) given
    in output HTML will be considered relative to the path of file
    which was initially called (the browser has no idea that
    some URLs were generated by files included from subfolders).


    Another cause could be wrapping URL parameters as fragments
    of path, eg.:
    http://some.domain.com/some_path/some_script.php/param1/param2
    In this case "some_script.php" in "some_path" is generating
    output HTML, but all URLs will be relative to virtual path
    http://some.domain.com/some_path/some_script.php/param1/param2



    In the example above the easiest solution would be to change
    relative path to the image file to server absolute path
    (you do not have to provide Internet absolute path, which
    means including protocol name and server name):
    <img src="/some_image.gif" />
    or (in the second case):
    <img src="/sub/some_image.gif" />
    It'll also work in case of path-wrapped parameters.


    Hilarion
    Hilarion Guest

  4. #4

    Default Re: Relative path & php

    Hilarion wrote: 
    > <div>
    > This is a main page.
    > </div>
    > </body>
    > </html>
    >
    > This file will display the image properly. But if we also have
    > subfolder called "sub" containing "index.php" file with:
    > <html>
    > <body>
    > <?php
    > include( '../top.inc' ); 
    > <div>
    > This is a subpage.
    > </div>
    > </body>
    > </html>
    >
    > Then the "sub/index.php" will not display the image properly
    > cause image tag contains relative path to "some_image.gif"
    > which is now relative to "sub" folder (from which we called
    > "index.php"). The fact that "top.inc" file is in root folder
    > is irrelevant here.[/ref]

    Umm - are you talking about indluded files, or directly accessing them?. The
    relative path should be from where the ROOT PHP doent is.

    (Ah - missed it earlier - you said that later in your post!)

    As an example, for clarification, I have the following file structure:

    / - contains the files "index.php" and "style.css"
    /images - contains all image files
    /includes - contains "header.php" and "footer.php"
    /pages - contains "page.php"

    If /pages/page.php is INCLUDED by "index.php" I need to use <img
    src="images/image.jpg"> to display the image. That's because the root file
    that INCLUDES pages/page.php is in the "/" directory, which is the base
    directory for the HTML.

    Now, if I were to open "pages/page.php" directly, the images wouldn't show
    up, since the root PHP doent is now in the sub-folder.
     

    Probably makes sense in this case - I would agree :)

    --
    Tony Garcia
    Web Right! Development
    Riverside, CA
    www.WebRightDevelopment.com


    Tony Guest

  5. #5

    Default Re: Relative path & php

    On Thu, 7 Jul 2005 19:40:31 +0200, "Hilarion"
    <op.SMIECI.pl> wrote:

    snip
     

    Thanks for the post.

    I am using the format you mention of:
     

    I think I have tried your suggestion, although I am not sure I follow
    you paragraph above. I have taken my code shown below

    <img src="library_files/logo.gif" alt="Mortgage Loans" border="0"
    width="128" height="122"></a><br><br></td>
    <a href="index.html"><font size="1">Home</font></a></p>

    and added a ../ so that it now read

    <img src="../library_files/logo.gif" alt="Mortgage Loans"
    border="0"
    width="128" height="122"></a><br><br></td>
    <a href="index.html"><font size="1">Home</font></a></p>

    library_files is a directory right under the root dir, and index.html
    is in the root.

    I am still not sure what you mean though. Could you take the code I
    show above and change the root to how you think it will work?

    I am lost on what is the server absolute path. And depending on what
    you mean is there a way to change the directory paths programatically
    without out having to rewrite my original code?

    PS I know how to fill in a php variable and pt the variable in front
    to create a correct path, as in

    <?

    $URL = "http://www.domain.com/";

    ?>

    <img src="<? echo $URL; ?>library_files/logo.gif" alt="Mortgage
    Loans" border="0"
    width="128" height="122"></a><br><br></td>
    <a href="<? echo $URL; ?>index.html"><font
    size="1">Home</font></a></p>

    I jsut would prefer to change or set the path at the beginning of a
    file rather than have to add the code to every file and link.

    Again thanks for all of the post.


    AF Guest

  6. #6

    Default Re: Relative path & php

    > I think I have tried your suggestion, although I am not sure I follow 


    If you use "../", then it's still relative path.
    If you are using parameter wrapping, then you should use
    absolute path (server absolute or Internet absolute):

    <img src="/library_files/logo.gif" alt="Mortgage Loans"
    border="0"
    width="128" height="122"></a><br><br></td>
    <a href="/index.html"><font size="1">Home</font></a></p>

    Notice that I used absolute path also for HREF.

     

    "index.html", "../dir/some_file.ext", "some_path/another.file" are
    relative paths and they are evaluated (when appear in HTML output)
    by web browser relatively to current doent path (warning:
    if current doent path ends with "/" sign, then it's treated
    as a folder, but if it's not, then last part is treated as file
    name and is stripped from path).
    If curren doent path is:
    http://some.domain.com/some_dir/some_doc.html
    then above paths lead to:
    http://some.domain.com/some_dir/index.html
    http://some.domain.com/dir/some_file.ext
    http://some.domain.com/some_dir/some_path/another.file
    If current path is:
    http://some.domain.com/another_dir/another_doc.php/param1/param2
    then above paths lead to:
    http://some.domain.com/another_dir/another_doc.php/param1/index.html
    http://some.domain.com/another_dir/another_doc.php/dir/some_file.ext
    http://some.domain.com/another_dir/another_doc.php/param1/some_path/another.file
    If current path is:
    http://some.domain.com/another_dir/another_doc.php/param1/param2/
    then above paths lead to:
    http://some.domain.com/another_dir/another_doc.php/param1/param2/index.html
    http://some.domain.com/another_dir/another_doc.php/param1/dir/some_file.ext
    http://some.domain.com/another_dir/another_doc.php/param1/param2/some_path/another.file

    If you are using paths with "../" inside them, then remember that it's
    evaluated on browser side, so you can't navigate this way above the
    webserver root.

    Absolute paths are:
    - server absolute path - starts with "/" sign, which means that all
    given paths are relative to webserver root folder (which may be and
    in most cases is different than server filesystem root),
    - internet absolute path - starts with "protocol" prefix eg. "http://",
    "mailto:" etc. and gives absolute and complete URL to resource.

     

    The best way would probably be to change all your relative URL paths
    to server absolute paths, but this requires rewriting most of your
    PHP / HTML code.

    You have other options which are not recomended but will work.
    They are based on <base> tag (inside <head> tag of <html> tag).
    If your <head> section is allready placed in one PHP/HTML file which is
    included by all other files, then using <base> tag would require only
    change in this one file. If not, then you'll have to change each
    file which contains (or should contain) <head> section.
    If you have http://some.domain.com/some_path/some_script.php doent
    which is called by http://some.domain.com/some_path/some_script.php/param1/param2
    URL and want all relative paths inside this doent be calculated
    basing on http://some.domain.com/some_path/some_script.php path, then
    you should place this path in <base> tag like this:
    <html>
    <head>
    <base href="http://some.domain.com/some_path/some_script.php" />
    ...
    </head>
    <body>
    ...
    </body>
    </html>
    If <head> section is defined in one file which is included by other PHP
    files which are located in different paths, then the way you should use
    <base> tag in it depends on the way you use relative paths in your
    doents. If the relative paths are relative to one specific folder
    (same for all doents), then you should use <base> as above, but
    if the paths are relative to the folder which contains the called
    doent, then base tag should be dynamically filled with path to
    called doent like this:
    <html>
    <head>
    <base href="http://some.domain.com<?php echo htmlspecialchars( $_SERVER[ 'SCRIPT_NAME' ] ); ?>" />
    ...
    </head>
    <body>
    ...
    </body>
    </html>


    Hilarion
    Hilarion Guest

  7. #7

    Default Re: Relative path & php

    On Fri, 8 Jul 2005 15:26:18 +0200, "Hilarion"
    <op.SMIECI.pl> wrote:

    snip
     
    snip 

    Thanks for the post and all of the info.

    Looks like I need to do some rewriting, as you point out above.

    I will play with some of the other ideas and techniques you have
    graciously written about. I think I understand the variuos path's now
    and how they work.

    Even if I can't do what I want to, I have learned a lot from
    everyone's post on this topic.

    Thanks again.

    AF Guest

Similar Threads

  1. relative path
    By Jan Eden in forum Macromedia Director 3D
    Replies: 9
    Last Post: January 20th, 07:18 PM

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