Why does relative include fail? [message #174448] |
Sun, 12 June 2011 01:00 |
Disc Magnet
Messages: 1 Registered: June 2011
Karma: 0
|
Junior Member |
|
|
disc@puff:~/php$ ls
a.php data include
disc@puff:~/php$ tree
.
├── a.php
├── data
│ └── d.php
└── include
├── b.php
└── c.php
2 directories, 4 files
disc@puff:~/php$ cat a.php
a.php is including include/b.php ...
<?php include "include/b.php" ?>
disc@puff:~/php$ cat include/b.php
b.php is including c.php and ../data/d.php ...
<?php include "c.php" ?>
<?php include "../data/d.php" ?>
disc@puff:~/php$ cat include/c.php
c.php
disc@puff:~/php$ cat data/d.php
d.php
disc@puff:~/php$ php a.php
a.php is including include/b.php ...
b.php is including c.php and ../data/d.php ...
c.php
PHP Warning: include(../data/d.php): failed to open stream: No
such file or directory in /home/disc/php/include/b.php on line 3
PHP Warning: include(): Failed opening '../data/d.php' for
inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/
disc/php/include/b.php on line 3
disc@puff:~/php$
Why does `include "c.php"` succeed but `include "../data/d.php"` fail?
http://www.php.net/manual/en/function.include.php mentions: "If a path
is defined — whether absolute (starting with a drive letter or \ on
Windows, or / on Unix/Linux systems) or relative to the current
directory (starting with . or ..) — the include_path will be ignored
altogether. For example, if a filename begins with ../, the parser
will look in the parent directory to find the requested file. "
Parent directory of what?
|
|
|
Re: Why does relative include fail? [message #174456 is a reply to message #174448] |
Sun, 12 June 2011 20:02 |
Mathieu Maes
Messages: 5 Registered: May 2011
Karma: 0
|
Junior Member |
|
|
On 12 jun, 03:00, Disc Magnet <discmag...@gmail.com> wrote:
> disc@puff:~/php$ ls
> a.php data include
>
> disc@puff:~/php$ tree
> .
> ├── a.php
> ├── data
> │ └── d.php
> └── include
> ├── b.php
> └── c.php
>
> 2 directories, 4 files
>
> disc@puff:~/php$ cat a.php
> a.php is including include/b.php ...
> <?php include "include/b.php" ?>
>
> disc@puff:~/php$ cat include/b.php
> b.php is including c.php and ../data/d.php ...
> <?php include "c.php" ?>
> <?php include "../data/d.php" ?>
>
> disc@puff:~/php$ cat include/c.php
> c.php
>
> disc@puff:~/php$ cat data/d.php
> d.php
>
> disc@puff:~/php$ php a.php
> a.php is including include/b.php ...
> b.php is including c.php and ../data/d.php ...
> c.php
> PHP Warning: include(../data/d.php): failed to open stream: No
> such file or directory in /home/disc/php/include/b.php on line 3
> PHP Warning: include(): Failed opening '../data/d.php' for
> inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/
> disc/php/include/b.php on line 3
> disc@puff:~/php$
>
> Why does `include "c.php"` succeed but `include "../data/d.php"` fail?
>
> http://www.php.net/manual/en/function.include.phpmentions: "If a path
> is defined — whether absolute (starting with a drive letter or \ on
> Windows, or / on Unix/Linux systems) or relative to the current
> directory (starting with . or ..) — the include_path will be ignored
> altogether. For example, if a filename begins with ../, the parser
> will look in the parent directory to find the requested file. "
>
> Parent directory of what?
Dear Disc Magnet,
Includes are always relative to the "parent" file that is including,
even when the include file is also including other files.
In your example, the correct include paths for b.php are as follows:
<?php include "include/c.php" ?>
<?php include "data/d.php" ?>
Kind regards,
Mathew
|
|
|
Re: Why does relative include fail? [message #174461 is a reply to message #174448] |
Sun, 12 June 2011 21:12 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 6/11/2011 9:00 PM, Disc Magnet wrote:
> disc@puff:~/php$ ls
> a.php data include
>
> disc@puff:~/php$ tree
> .
> ├── a.php
> ├── data
> │ └── d.php
> └── include
> ├── b.php
> └── c.php
>
> 2 directories, 4 files
>
> disc@puff:~/php$ cat a.php
> a.php is including include/b.php ...
> <?php include "include/b.php" ?>
>
> disc@puff:~/php$ cat include/b.php
> b.php is including c.php and ../data/d.php ...
> <?php include "c.php" ?>
> <?php include "../data/d.php" ?>
>
> disc@puff:~/php$ cat include/c.php
> c.php
>
> disc@puff:~/php$ cat data/d.php
> d.php
>
> disc@puff:~/php$ php a.php
> a.php is including include/b.php ...
> b.php is including c.php and ../data/d.php ...
> c.php
> PHP Warning: include(../data/d.php): failed to open stream: No
> such file or directory in /home/disc/php/include/b.php on line 3
> PHP Warning: include(): Failed opening '../data/d.php' for
> inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/
> disc/php/include/b.php on line 3
> disc@puff:~/php$
>
> Why does `include "c.php"` succeed but `include "../data/d.php"` fail?
>
> http://www.php.net/manual/en/function.include.php mentions: "If a path
> is defined — whether absolute (starting with a drive letter or \ on
> Windows, or / on Unix/Linux systems) or relative to the current
> directory (starting with . or ..) — the include_path will be ignored
> altogether. For example, if a filename begins with ../, the parser
> will look in the parent directory to find the requested file. "
>
> Parent directory of what?
In addition to what Mathieu said, a better way is to always use paths
relative $_SERVER['DOCUMENT_ROOT']. This will end up as an absolute
path on the server, but always relative the same point - the document
root specified in your server configuration.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|