php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #30881 C++ like include (path/search) in PHP?
Submitted: 2004-11-24 14:25 UTC Modified: 2004-11-26 15:41 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: ovdspek at liacs dot nl Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.3.9 OS: Debian Linux Sarge
Private report: No CVE-ID: None
 [2004-11-24 14:25 UTC] ovdspek at liacs dot nl
Description:
------------
In PHP, includes (appear to) use the current working directory. In C++, includes use the directory of the including script (and the include path).

Could the C++ behaviour be added to PHP includes?
Because if relative includes are used, current PHP scripts can only be executed if cwd == script dir.

Reproduce code:
---------------
$ cat 1.php 
<?php
        echo "1.php<br>\n";
?>

$ cat b/2.php 
<?php
        include('../1.php');
?>

$ php -f b/2.php 

Warning: main(../1.php): failed to open stream: No such file or directory in /home/olaf/t/a/b/2.php on line 2

Warning: main(): Failed opening '../1.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/olaf/t/a/b/2.php on line 2

$ cd b

$ php -f b/2.php 
Could not open input file: b/2.php.

$ php -f 2.php 
1.php<br>

$ 

Expected result:
----------------
Warning: main(../1.php): failed to open stream: No such file or directory in /home/olaf/t/a/b/2.php on line 2

Warning: main(): Failed opening '../1.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/olaf/t/a/b/2.php on line 2


Actual result:
--------------
1.php<br>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-11-24 14:29 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This is expected, and we can not change this as it will break a lot of scripts.
 [2004-11-24 14:33 UTC] ovdspek at liacs dot nl
I'm asking for an addition, not a modification.

What kind of scripts would be broken if you search additional paths (after searching in the old way)?
 [2004-11-24 14:34 UTC] ovdspek at liacs dot nl
And doesn't the documentation claim to already search relative to the script dir?

> and then in include_path relative to the directory of current script.
 [2004-11-24 14:45 UTC] ovdspek at liacs dot nl
Oops, forgot to reopen.
 [2004-11-25 22:55 UTC] tony2001@php.net
PHP CLI "does not change the working directory to that of the script" (c) http://www.php.net/manual/en/features.commandline.php
In your case documentation says about other SAPIs.
 [2004-11-25 23:59 UTC] ovdspek at liacs dot nl
I'm not asking for PHP (CLI) to change the cwd to that of the script.
I'm only asking for PHP to look for files *relative to the directory of the current script*, as the documentation claims it does.
Note that the directory of the current script is *not* equivalent to the cwd (current working directory).

> Files for including are first looked in include_path relative to the current working directory and then in include_path relative to the directory of current script.
 [2004-11-26 01:33 UTC] edink@php.net
All other SAPIs will chdir to the scripts directory. CLI will not. This is not a bug, so please don't reopen this report. 

You can always use:

include dirname(__FILE__).'/../1.php';

to solve your problem with relative paths in includes.
 [2004-11-26 09:29 UTC] ovdspek at liacs dot nl
> All other SAPIs will chdir to the scripts directory. CLI will not.

Isn't that what Tony said too?
 [2004-11-26 09:42 UTC] derick@php.net
That doesn't matter. It will NOT change and NOT added. Please stop reopening this bug.
 [2004-11-26 10:12 UTC] ovdspek at liacs dot nl
In that case, please update your documentation so it doesn't say it'll search relative to the directory of current script.
 [2004-11-26 10:53 UTC] tony2001@php.net
>In that case, please update your documentation so it 
>doesn't say it'll search relative to the directory of 
>current script.
All SAPIs except CLI DO search relative to the directory of current running script.
But you're using CLI and this feature is documented too.
 [2004-11-26 11:10 UTC] ovdspek at liacs dot nl
So exactly where is that documented?
At http://php.net/include/ I see no exceptions for CLI and at http://php.net/manual/en/features.commandline.php I see no exceptions for include.

It does say "It does not change the working directory to that of the script. (-C and --no-chdir switches kept for compatibility)", but that's not relevant, as the include docs say it searches the directory of the current script, not the current working directory.
If your implementation relies on the current working directory being the directory of the current script, then your implementation (of this docs/specs) is wrong.
 [2004-11-26 11:29 UTC] vrana@php.net
"Files for including are first looked in include_path relative to the current working directory and then in include_path relative to the directory of current script." in include() page. Current working directory can be something different than directory of a script.
 [2004-11-26 11:39 UTC] ovdspek at liacs dot nl
> Current working
directory can be something different than directory of a script.

I understand, but apparently your implementation doesn't.
Why else would "include('../1.php');" fail?
 [2004-11-26 15:22 UTC] vrana@php.net
You are right, documentation is not absolutely clear. Documented behavior is valid only if you don't go to upper directory in include.

b/2.php: <?php include "c/3.php"; ?>
b/c/3.php: b/c/3
`php b/2.php` works as documented - prints b/c/3 (if c/3.php doesn't exist).

I will update the docs.
 [2004-11-26 15:40 UTC] vrana@php.net
"If filename begins with <literal>../</literal>, it is looked only in include_path relative to the current working directory."
 [2004-11-26 15:41 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jun 30 13:01:29 2024 UTC