note 56419 added to function.include
| From: | tj at ranunes dot com | Date: | Thu, 01 Sep 2005 23:51:36 +0000 |
| Subject: | note 56419 added to function.include | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-94632@lists.php.net to get a copy of this message | ||
Script to determine the location of a file that I am trying to include.
a. What is the path of the file in relation to the current directory, and assuming that the file
name is relative to the scripts location?
include(FindScriptRelativeToCurentDirectory(dirname(realpath (__FILE__))) .
"relpath/myIncFile.php");
where "relpath/myIncFile.php" is the name of the file that you are trying to include
relative to your script (the script where the include() resides).
The path returned is terminated with a slash.
b. What is the path of the file in relation to the current directory, and assuming that the file
name is relative to the doc root?
include(FindScriptRelativeToCurentDirectory("myFile", true, true));
the second parameter tells the function that myFile is relative to the doc root (instead of the
executing script's path), and the second parameter tells the function that myFile contains a
file name (it is not just a path).
/*
**** Get relative path of this file (relative to current directory)
$scriptPathOrName - either the path or name of the script that you are trying to find.
$scriptPathIsRelativeToDocRoot - if this is false, it is assumed that $scriptPathOrName is relative
to the script currently executing. Otherwise, it is relative to
doc root.
$thisIsAFileName - If true, then $scriptPathOrName includes the file name.
Otherwise, it is just a path.
*/
function FindScriptRelativeToCurentDirectory($scriptPathOrName,
$scriptPathIsRelativeToDocRoot=false, $thisIsAFileName=false) {
$currentPath = realpath ('.');
$commonPathPtr = 0;
/* Find the character used to separate dir names (backslash in windows, forwardslash in Linux, etc.)
*/
$dirChar = "";
for ($i=0; $i < strlen($currentPath ); $i++) {
if ($currentPath{$i} == '/' or $currentPath{$i} == '\\') {
$dirChar = $currentPath{$i};
break;
}
}
/* If script is relative to doc root, combine the two */
if ($scriptPathIsRelativeToDocRoot) {
$rootPath = $_SERVER['DOCUMENT_ROOT'];
if($rootPath{strlen($rootPath)-1} != $dirChar) {
$rootPath .= $dirChar;
}
if ($scriptPathOrName{0} == '/' or $scriptPathOrName{0} == '\\') {
$scriptPathOrName = substr($scriptPathOrName, 1, strlen($scriptPathOrName) - 1);
}
$scriptPathOrName = $rootPath . $scriptPathOrName;
}
/* because you may developing in different systems that where you are going to deploy,
there may be a mix of backslashes and forwardslashes when combining the root path
with a script name. We need to insure consistency. */
if ($dirChar == '/') {
$scriptPathOrName = str_replace ('\\', $dirChar, $scriptPathOrName);
} else {
$scriptPathOrName = str_replace ('/', $dirChar, $scriptPathOrName);
}
/* Make sure that the paths end on slash */
if($currentPath{strlen($currentPath)-1} != $dirChar) {
$currentPath .= $dirChar;
}
if($scriptPathOrName{strlen($scriptPathOrName)-1} != $dirChar) {
$scriptPathOrName .= $dirChar;
}
/* Find out where it is that the two paths deviate */
for ($i=0; $i < strlen($currentPath) && $i < strlen($scriptPathOrName); $i++) {
if (strtolower($currentPath{$i}) != strtolower($scriptPathOrName{$i})) {
break;
}
if ($currentPath{$i} == $dirChar) {
$commonPathPtr = $i;
}
}
/* Navigate up the current directory to the node where the two directory trees were the same */
$relativePath = "." . $dirChar;
for (; $i < strlen($currentPath); $i++) {
if ($currentPath{$i} == $dirChar) {
$relativePath .= ".." . $dirChar;
}
}
/* Now append the directory down from the common node to where this file is at */
if ($commonPathPtr < strlen($scriptPathOrName)-1) {
$relativePath .= substr($scriptPathOrName, $commonPathPtr+1,
strlen($scriptPathOrName)-$commonPathPtr-1);
}
/* Remove trailing slash if this was a filename */
if ($thisIsAFileName) {
$relativePath= substr($relativePath, 0, strlen($relativePath) - 1);
}
return($relativePath);
}
----
Manual Page -- http://www.php.net/manual/en/function.include.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56419
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56419&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56419&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56419&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56419&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56419&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56419&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56419&report=yes
Search -- http://master.php.net/manage/user-notes.php