php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #11326 Troubles with nested include()'s (bug #9673 revisited)
Submitted: 2001-06-07 07:16 UTC Modified: 2001-07-16 10:39 UTC
From: joschlec at debian dot org Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.5 OS: Debian GNU/Linux
Private report: No CVE-ID: None
 [2001-06-07 07:16 UTC] joschlec at debian dot org
Please do not immediately file this bug as a duplicate of 
#9673. Although my bug's goal is reporting the same 
problem, it appears that #9673 was last 'Anylized' three 
months ago with no reply to the last comment made by the 
bug's author.

PHP desperately needs a way to do nested includes, 
preserving not only php relative paths transparently, but 
server side paths as well. By server side path, I am 
refering to <script src="js/myscript.js"> and <img 
src="images/myimage.gif">. Currently, if one includes a 
file by means of include(), include_once(), etc., the 
working directory of the included file, no matter where it 
is located, is of the calling file. One can circumvent 
this obstacle a couple of ways. The easiest appears to be 
the method of changing one's working directory as you move 
into an included file. Of course, one must always remember 
to change the working directory back to the original 
location after the file has been included.

Unfortunately, this work around breaks down when one 
wishes to use server side paths in an included file. 
Although the working directory has been changed, the 
server working directory is the same. The following is an 
example that completely illustrates the problem.

EXAMPLE DIRCTORY STUCTURE:
	/ --> a.php
	/b/ --> b1.php
	/b/ --> b2.php
	/b/images/ --> php_logo.gif
	

BUG:
	/*-- /a.php --*/
	print( "<p>a.php " . getcwd() . "</p>");
	include_once( "b/b1.php" );

	/*-- /b/b1.php --*/
	print( "<p>b1.php " . getcwd() . "</p>" );
	include_once( "b2.php" );

	/*-- /b/b2.php --*/
	print( "<p>b2.php " . getcwd() . "</p>" );
	print( "<img src=images/php_logo.gif>" );

ACCESS: http://server/a.php
RESULT:
	a.php /
	b1.php /
	Warning: Failed opening 'b2.php' for inclusion
	(include_path='.:/usr/lib/php4') in
	/b/b1.php on line 3

WORK AROUND:
	/*-- /a.php --*/
	print( "<p>a.php " . getcwd() . "</p>");
	chdir( "b" );
	include_once( "b1.php" );
	chdir( ".." );

	/*-- /b/b1.php --*/
	print( "<p>b1.php " . getcwd() . "</p>" );
	include_once( "b2.php" );

	/*-- /b/b2.php --*/
	print( "<p>b2.php " . getcwd() . "</p>" );
	print( "<img src=images/php_logo.gif>" );

ACCESS: http://server/a.php
RESULT:
	a.php /var/www/php_work_around
	b1.php /var/www/php_work_around/b
	b2.php /var/www/php_work_around/b
	[image not found]

Of course, the image is not showing up because the server 
thinks it is located at /images/php_logo.gif. When it is 
really located at /b/images/php_logo.gif!

In bug #9673, on 2001-03-15 09:08:11 stas@php.net writes 
"Now, all relative pathes are resolved against the current 
directory of the including script (which is the directory 
where it's located). This is a known issue. Use 
include_pathes in the meantime."

Unfortunately, three months later "This is [still] a known 
issue" and stas@php.net is still correct!

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-06-13 17:26 UTC] cardinal@php.net
Without addressing the server-side issues, I'll point out
why the client side ones are almost certain not to change.

<script src="js/myscript.js"> and <img src="images/php_logo.gif">
are both ignored by the PHP interpreter.

The browser (Which will be processing the tags) does not
konw PHP was involved in the document's creation.

> Of course, the image is not showing up because the server
> thinks it is located at /images/php_logo.gif. When it is
> really located at /b/images/php_logo.gif!

As mentioned above, the server isn't thinking anything
about the image.  The browser thinks it's in the images
subdirectory, relative to the path of the html page it's
parsing.  The fact that this <img> tag came from a file
that's in a subdirectory (On the server) wouldn't be
relevant even if PHP was changing the working directory as
you want it to, because PHP won't be interpreting the html
code (Nor should it).
 [2001-06-14 14:33 UTC] joschlec at debian dot org
*OOPS* After submitting this bug, the next day I realized 
my argument flaw about what I called "the server side 
path" (which is actually the client side path, as 
cardinal@php.net pointed out). Please disregard it, I 
don't know what I was thinking ;-)

However, I still stand very firm on the idea that php 
needs to change its implementation of included files.

 [2001-06-15 07:05 UTC] zeev@php.net
Just to see that I understand correctly, will the following userland function solve the problem?

function my_include($file)
{
    $orig_dir = getcwd();
    chdir(dirname($file));
    $retval = include("./".basename($file));
    chdir($orig_dir);
    return $retval;
}

If so, you can simply use this solution in the meantime.  We may want to change PHP to use this behavior, but if we do, it'd have to happen in a major version, because it's a major change.
 [2001-07-16 10:39 UTC] zeev@php.net
From now on (CVS/4.0.7), include() will also look in the directory of the currently executing file.  This should give you the functionality you asked for.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 14:01:28 2024 UTC