note 118083 added to function.include

From: Date: Thu, 01 Oct 2015 09:40:01 +0000
Subject: note 118083 added to function.include
Groups: php.notes 
Request: Send a blank email to php-notes+get-203884@lists.php.net to get a copy of this message
When including a file using its name directly without specifying we are talking about the current working directory, i.e. saying (include "file") instead of ( include "./file") . PHP will search first in the current working directory (given by getcwd() ) , then next searches for it in the directory of the script being executed (given by __dir__). This is an example to demonstrate the situation : We have two directory structure : -dir1 ----script.php ----test ----dir1_test -dir2 ----test ----dir2_test dir1/test contains the following text : This is test in dir1 dir2/test contains the following text: This is test in dir2 dir1_test contains the following text: This is dir1_test dir2_test contains the following text: This is dir2_test script.php contains the following code: <?php echo 'Directory of the current calling script: ' . __DIR__; echo '<br />'; echo 'Current working directory: ' . getcwd(); echo '<br />'; echo 'including "test" ...'; echo '<br />'; include 'test'; echo '<br />'; echo 'Changing current working directory to dir2'; chdir('../dir2'); echo '<br />'; echo 'Directory of the current calling script: ' . __DIR__; echo '<br />'; echo 'Current working directory: ' . getcwd(); echo '<br />'; echo 'including "test" ...'; echo '<br />'; include 'test'; echo '<br />'; echo 'including "dir2_test" ...'; echo '<br />'; include 'dir2_test'; echo '<br />'; echo 'including "dir1_test" ...'; echo '<br />'; include 'dir1_test'; echo '<br />'; echo 'including "./dir1_test" ...'; echo '<br />'; (@include './dir1_test') or die('couldn\'t include this file '); ?> The output of executing script.php is : Directory of the current calling script: C:\dev\www\php_experiments\working_directory\example2\dir1 Current working directory: C:\dev\www\php_experiments\working_directory\example2\dir1 including "test" ... This is test in dir1 Changing current working directory to dir2 Directory of the current calling script: C:\dev\www\php_experiments\working_directory\example2\dir1 Current working directory: C:\dev\www\php_experiments\working_directory\example2\dir2 including "test" ... This is test in dir2 including "dir2_test" ... This is dir2_test including "dir1_test" ... This is dir1_test including "./dir1_test" ... couldn't include this file ---- Server IP: 72.52.91.14 Probable Submitter: 197.160.97.206 ---- Manual Page -- http://php.net/manual/en/function.include.php Edit -- https://master.php.net/note/edit/118083 Del: integrated -- https://master.php.net/note/delete/118083/integrated Del: useless -- https://master.php.net/note/delete/118083/useless Del: bad code -- https://master.php.net/note/delete/118083/bad+code Del: spam -- https://master.php.net/note/delete/118083/spam Del: non-english -- https://master.php.net/note/delete/118083/non-english Del: in docs -- https://master.php.net/note/delete/118083/in+docs Del: other reasons-- https://master.php.net/note/delete/118083 Reject -- https://master.php.net/note/reject/118083 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#203884) next »