Re: include question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2009/3/6 Daniel Brown <danbrown@xxxxxxx>

> On Fri, Mar 6, 2009 at 08:37, PJ <af.gourmet@xxxxxxxxxxxx> wrote:
> > good morning all,
> >
> > How can I include src and href in include files that will refer the
> > right paths from files in different hierarchies(directory tree levels)?
> > Example:
> > include dirname(_FILE_)."/../lib/header1.php"; ?
> >
> > This does not work:
> > snippetysnip...
> > <LINK href="dirname(_FILE_).'/../lib/index.css'" rel="stylesheet"
> > type="text/css">
> > </head>
> > Nor does this: (NOTE THE DIFFERENCES " AND ' IN THE SRC AND HREF)
> > <div id="frame">
> >    <IMG SRC=dirname(_FILE_)."/../images/bannerbae1.gif" ...snippetysnip
> > Perhaps it's not possible?
>
>     It is when it's parsed by PHP.  You're just putting it through as
> straight HTML, which - even though it may have a .php extension -
> won't work as you might be expecting.
>
>    Instead, you have to instruct the parsing engine where and when to
> interpret, compile, and execute the code, like so:
>
> <img src="<?php echo dirname(dirname(__FILE__)); ?>/images/bannerbae1.gif">
>
>    In the above snippet, notice a few very important things:
>
>        1.) We use regular open tags to be compatible with all stock
> PHP configurations.
>        2.) We echo out the response from dirname() so that it's
> output to the HTML source.
>        3.) We use dirname() twice, so it gives the dirname() of the
> dirname(), rather than '..'.
>        4.) There are double underscores around FILE.  The same is
> true with LINE, FUNCTION, etc.


5.) dirname() gives you the full path on disk, not the URL. Usually you can
just remove the document root path to get the URL. This could be in
$_SERVER['DOCUMENT_ROOT'] but you can't rely on that between servers or when
the config changes.
-Stuart

-- 
http://stut.net/

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux