note 97729 added to function.include

From: Date: Wed, 05 May 2010 14:04:21 +0000
Subject: note 97729 added to function.include
Groups: php.notes 
Request: Send a blank email to php-notes+get-168961@lists.php.net to get a copy of this message
Problems with including files inside of files included by other files (Tim describes this problem as well) originates from an inconsistency in PHP and how it handles including files. Given a file B that is included in file A, any time you include files in B and the path begins with "../" the path is no longer relative to B it is relative to A. (It sounds silly I know) I tested this in PHP 5.* and it's consistently the problems most people have with this. Note, there isn't a problem when your path does not begin with "../". Now, I have found a trick to fix this problem. And (although no offense to Tim and others who offered great solutions) there is a much more elegant way of making this work. Following my above example, with file A and B to fix the relative pathing do the following: 1. Create a dummy directory in the same directory as B (let's call it dummyDir) 2. Append "dummDir/../" to the beginning of include paths in B. 3. And now the path will work fine. Ex. originally in B: include("../lib/functions.php"); would not work if there wasn't a directory above A that contained a directory "lib". Now we do: include("dummyDir/../../lib/functions.php"); That will now work as intended. ---- Server IP: 208.69.120.55 Probable Submitter: 216.94.129.143 ---- Manual Page -- http://www.php.net/manual/en/function.include.php Edit -- https://master.php.net/note/edit/97729 Del: integrated -- https://master.php.net/note/delete/97729/integrated Del: useless -- https://master.php.net/note/delete/97729/useless Del: bad code -- https://master.php.net/note/delete/97729/bad+code Del: spam -- https://master.php.net/note/delete/97729/spam Del: non-english -- https://master.php.net/note/delete/97729/non-english Del: in docs -- https://master.php.net/note/delete/97729/in+docs Del: other reasons-- https://master.php.net/note/delete/97729 Reject -- https://master.php.net/note/reject/97729 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#168961) next »