"; } And suppose I place this function in /chung/utilities/useless/functions.php. When I pass a relative path to this function in some project, I certainly wouldn't want the path to be relative to the file containing the function. [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => <1aqdnf_JY7jIyfLdRVn-tA@comcast.com> [ref] => [htmlstate] => on_nl2br [postusername] => Chung Leong [ip] => chernyshevsky@h [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 7 [islastshown] => [isfirstshown] => [attachments] => [allattachments] => ) -->"; > } > > And suppose I place this function in /chung/utilities/useless/functions.php. > When I pass a relative path to this function in some project, I certainly > wouldn't want the path to be relative to the file containing the function.[/quote] good point. -d [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => [ref] => <1aqdnf_JY7jIyfLdRVn-tA@comcast.com> [htmlstate] => on_nl2br [postusername] => Doug [ip] => dougd99@XXXeart [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 8 [islastshown] => [isfirstshown] => [attachments] => [allattachments] => ) --> relative include paths? What's the use? - PHP Development

relative include paths? What's the use? - PHP Development

If I were to write an include with a relative path like include("../conf/config.php"); What is the use? As far as I understand it, the path is relative to the first script that is called by php. In other words, if the current working directory is /www/ and you were "executing" a.php in that directory, then a.php included /www/include/b.php by doing include("include/b.php");, then b.php tried to include file /www/config/c.php, by doing include("../config/c.php) it would not work. Instead php would be looking in /config for c.php and it would not find it (?!). Relative paths in include directives seem useless, because the ...

  1. #1

    Default relative include paths? What's the use?


    If I were to write an include with a relative path like

    include("../conf/config.php");

    What is the use?

    As far as I understand it, the path is relative to the first script that
    is called by php.

    In other words, if the current working directory is /www/ and you were
    "executing" a.php in that directory, then a.php included
    /www/include/b.php by doing include("include/b.php");, then b.php tried
    to include file /www/config/c.php, by doing include("../config/c.php) it
    would not work. Instead php would be looking in /config for c.php and
    it would not find it (?!).

    Relative paths in include directives seem useless, because the paths are
    not relative to the file that the include directive is in, like you
    would expect them to be. These includes are totally dependant on where
    the original file was "run" from.

    Can any one shed some light on why relative paths in includes are of
    -any- practical use in PHP? Or, is this a bug?

    PHP 4.3.4 (cli) (built: Mar 9 2004 11:40:14)

    By the way, I am just trying to separate my php project files into a few
    directories. I just want to be able to include them without using
    absolute paths. These files in these different directories should be
    able to include each other.

    -d

    Doug Guest

  2. #2

    Default Re: relative include paths? What's the use?

    "Doug" <dougd99earthlink.net> wrote in message
    news:rsqbc.9130$yN6.6152newsread2.news.atl.earthl ink.net...
    >
    > If I were to write an include with a relative path like
    >
    > include("../conf/config.php");
    >
    > What is the use?
    >
    > As far as I understand it, the path is relative to the first script that
    > is called by php.
    That's because in PHP, an include is an runtime operation and not a
    preprocess macro expansion. As an operation, it's perfectly logical that
    relative paths are relative to the runtime path and not the source path.
    This is certainly not a bug.

    To do multiple level relative includes, you can use __FILE__ to determine
    the location of the current file, and append the directory path in the
    include statement. Example:

    <?

    $CURRENT_SOURCE_FOLDER = dirname(__FILE__);

    require_once("$CURRENT_SOURCE_FOLDER/grumpy.php");
    require_once("$CURRENT_SOURCE_FOLDER/sleepy.php");
    require_once("$CURRENT_SOURCE_FOLDER/sneezy.php");

    ....


    Chung Leong Guest

  3. #3

    Default Re: relative include paths? What's the use?

    In article <rsqbc.9130$yN6.6152newsread2.news.atl.earthlink. net>,
    Doug <dougd99earthlink.net> wrote:
    > Can any one shed some light on why relative paths in includes are of
    > -any- practical use in PHP? Or, is this a bug?
    I use them in the following way:

    The first file included in every script is a small config file (in the
    same directory as the script itself, so it's guaranteed to be found in a
    default PHP setup) which sets the include paths. In that file I have
    something like this:

    ini_set('include_path', ini_get('include_path') . ':' .
    realpath('relative/path/to/include_directory'));

    Because of the 'realpath' function, which translates relative paths to
    absolute paths, I can copy the directory tree of my application to
    another machine and everything will keep working, even if the directory
    structure 'above' my application is different.

    JP

    --
    Sorry, <devnullcauce.org> is een "spam trap".
    E-mail adres is <jpk"at"akamail.com>, waarbij "at" = .
    Jan Pieter Kunst Guest

  4. #4

    Default Re: relative include paths? What's the use?

    Ok, but how often do you find that even though you don't use relative paths,
    your sites doesn't work? Even if the top structure is different from your
    development PC than your server / ISP server? I haven't had to use it yet.
    In ASP yes, but not really in PHP

    --

    Kind Regards
    Rudi Ahlers
    +27 (82) 926 1689

    Greater love has no one than this, that he lay down his life for his friends
    (John 15:13).
    "Jan Pieter Kunst" <devnullcauce.org> wrote in message
    news:devnull-4C0B3E.17212403042004news1.news.xs4all.nl...
    In article <rsqbc.9130$yN6.6152newsread2.news.atl.earthlink. net>,
    Doug <dougd99earthlink.net> wrote:
    > Can any one shed some light on why relative paths in includes are of
    > -any- practical use in PHP? Or, is this a bug?
    I use them in the following way:

    The first file included in every script is a small config file (in the
    same directory as the script itself, so it's guaranteed to be found in a
    default PHP setup) which sets the include paths. In that file I have
    something like this:

    ini_set('include_path', ini_get('include_path') . ':' .
    realpath('relative/path/to/include_directory'));

    Because of the 'realpath' function, which translates relative paths to
    absolute paths, I can copy the directory tree of my application to
    another machine and everything will keep working, even if the directory
    structure 'above' my application is different.

    JP

    --
    Sorry, <devnullcauce.org> is een "spam trap".
    E-mail adres is <jpk"at"akamail.com>, waarbij "at" = .


    Rudi Ahlers Guest

  5. #5

    Default Re: relative include paths? What's the use?



    Chung Leong wrote:
    > "Doug" <dougd99earthlink.net> wrote in message
    > news:rsqbc.9130$yN6.6152newsread2.news.atl.earthl ink.net...
    >
    >>If I were to write an include with a relative path like
    >>
    >>include("../conf/config.php");
    >>
    >>What is the use?
    >>
    >>As far as I understand it, the path is relative to the first script that
    >>is called by php.
    >
    >
    > That's because in PHP, an include is an runtime operation and not a
    > preprocess macro expansion. As an operation, it's perfectly logical that
    > relative paths are relative to the runtime path and not the source path.
    > This is certainly not a bug.
    >
    > To do multiple level relative includes, you can use __FILE__ to determine
    > the location of the current file, and append the directory path in the
    > include statement. Example:
    >
    > <?
    >
    > $CURRENT_SOURCE_FOLDER = dirname(__FILE__);
    >
    > require_once("$CURRENT_SOURCE_FOLDER/grumpy.php");
    > require_once("$CURRENT_SOURCE_FOLDER/sleepy.php");
    > require_once("$CURRENT_SOURCE_FOLDER/sneezy.php");


    Regardless of the implementatation details (runtime vs. preprocess),
    they could've done it either way. Just like the makers of gcc could
    have chose to do it either way. The PHP programmers chose to make the
    path relative to the first script that is called by php.

    In your example, you changed the paths to be absolute. That makes it so
    that the file included would be the file that most people expect will be
    included in the first place.

    Could some one give me a practical example of a -relative- path being
    used in PHP?


    -d

    Doug Guest

  6. #6

    Default Re: relative include paths? What's the use?

    In article <r8idnTOMSfZ9fvPdRVn-hgis.co.za>,
    "Rudi Ahlers" <SP4M_RudiSP4M_Bonzai.org.za_SP4M> wrote:
    > Ok, but how often do you find that even though you don't use relative paths,
    > your sites doesn't work? Even if the top structure is different from your
    > development PC than your server / ISP server? I haven't had to use it yet.
    > In ASP yes, but not really in PHP
    Well, on my development machine I have my sites in my home folder
    (~/Sites on Mac OS X) and on the server (also Mac OS X) they are in
    /Library/WebServer/Doents.

    It was also very convenient when we went from Linux (webserver in
    /usr/local/httpd/htdocs) to Mac OS X -- I could simply copy my sites
    without breaking anything.

    JP

    --
    Sorry, <devnullcauce.org> is een "spam trap".
    E-mail adres is <jpk"at"akamail.com>, waarbij "at" = .
    Jan Pieter Kunst Guest

  7. #7

    Default Re: relative include paths? What's the use?

    "Doug" <dougd99earthlink.net> wrote in message
    news:HDCbc.9139$NL4.2247newsread3.news.atl.earthl ink.net...
    > Regardless of the implementatation details (runtime vs. preprocess),
    > they could've done it either way. Just like the makers of gcc could
    > have chose to do it either way. The PHP programmers chose to make the
    > path relative to the first script that is called by php.
    The fact that it's an runtime operation makes a big difference. Suppose I
    write a function that include a file:

    function CustomInclude($path) {
    include($path);
    echo "<!-- including $path -->";
    }

    And suppose I place this function in /chung/utilities/useless/functions.php.
    When I pass a relative path to this function in some project, I certainly
    wouldn't want the path to be relative to the file containing the function.


    Chung Leong Guest

  8. #8

    Default Re: relative include paths? What's the use?



    Chung Leong wrote:
    > "Doug" <dougd99earthlink.net> wrote in message
    > news:HDCbc.9139$NL4.2247newsread3.news.atl.earthl ink.net...
    >
    >>Regardless of the implementatation details (runtime vs. preprocess),
    >>they could've done it either way. Just like the makers of gcc could
    >>have chose to do it either way. The PHP programmers chose to make the
    >>path relative to the first script that is called by php.
    >
    >
    > The fact that it's an runtime operation makes a big difference. Suppose I
    > write a function that include a file:
    >
    > function CustomInclude($path) {
    > include($path);
    > echo "<!-- including $path -->";
    > }
    >
    > And suppose I place this function in /chung/utilities/useless/functions.php.
    > When I pass a relative path to this function in some project, I certainly
    > wouldn't want the path to be relative to the file containing the function.
    good point.

    -d

    Doug Guest

  9. #9

    Lightbulb Relative include paths? What's the use?

    I created a code to always make the links in the included file as relative. This takes care of the nested directories. To do this, first add the following code to the file that is included.

    $path_array = explode("/", $_SERVER[SCRIPT_FILENAME]);
    $my_rel_html_path = "";
    if (count($path_array) == 5) /* 5 would be the length if you look at the regular path name in web server. In mine it was /home/abc/public_html. When this is exploded it is 4 length of array. The fifth is the fact that when you explode SCRIPT_FILENAME, it will include the file name as well. In essence, based on the server setup, you will just need to tweak the digit 5 to which ever location is the root directory of the website.*/
    {
    $my_rel_html_path = "./";
    }
    else
    {
    for ($i=5; $i < count($path_array); $i++)
    {
    $my_rel_html_path .= "../";
    }
    }
    echo $my_rel_html_path;


    Then use the $my_rel_html_path before any link value in HTML. For example,

    <img src="<?php echo($my_rel_html_path);?>images/arrow.jpg">
    OR
    <a href="<?php echo($my_rel_html_path);?>home.php">

    No matter which page is calling the included file, the links in the included file will always work.
    Unregistered Guest

Similar Threads

  1. Absolute vs. Relative Paths?
    By Westmatrix99 in forum Macromedia Exchange Dreamweaver Extensions
    Replies: 2
    Last Post: April 16th, 05:19 PM
  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. Replies: 0
    Last Post: November 15th, 06:39 PM
  5. Replies: 0
    Last Post: November 15th, 12:52 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
  •