note 81517 added to function.getcwd
| From: | znupi69NOSPAMHERE at gmail dot com | Date: | Sun, 02 Mar 2008 15:01:48 +0000 |
| Subject: | note 81517 added to function.getcwd | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-136660@lists.php.net to get a copy of this message | ||
When running PHP on the command line, if you want to include another file which is in the same
directory as the main script, doing just
<?php
include './otherfile.php';
?>
might not work, if you run your script like this:
/$ /path/to/script.php
because the current working dir will be set to '/', and the file
'/otherfile.php' does not exist, because it is in '/path/to/otherfile.php'.
So, to get the directory in which the script resides, you can use this function:
<?php
function get_file_dir() {
global $argv;
$dir = dirname(getcwd() . '/' . $argv[0]);
$curDir = getcwd();
chdir($dir);
$dir = getcwd();
chdir($curDir);
return $dir;
}
?>
So you can use it like this:
<?php
include get_file_dir() . '/otherfile.php';
// or even..
chdir(get_file_dir());
include './otherfile.php';
?>
Spent some time thinking this one out, maybe it helps someone :)
----
Server IP: 69.147.83.197
Probable Submitter: 77.81.204.37
----
Manual Page -- http://www.php.net/manual/en/function.getcwd.php
Edit -- https://master.php.net/note/edit/81517
Del: integrated -- https://master.php.net/note/delete/81517/integrated
Del: useless -- https://master.php.net/note/delete/81517/useless
Del: bad code -- https://master.php.net/note/delete/81517/bad+code
Del: spam -- https://master.php.net/note/delete/81517/spam
Del: non-english -- https://master.php.net/note/delete/81517/non-english
Del: in docs -- https://master.php.net/note/delete/81517/in+docs
Del: other reasons-- https://master.php.net/note/delete/81517
Reject -- https://master.php.net/note/reject/81517
Search -- https://master.php.net/manage/user-notes.php