|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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! PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 04:00:01 2025 UTC |
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.