得到绝对目录路径? [英] getting absolute directory path?

查看:71
本文介绍了得到绝对目录路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好 -


我有两个问题。


首先,我在/ home / user下有一个网站/万维网/。 index.php

和所有其他网站页面都在/ home / user / www /下。对于在多个文件中使用的

函数,我有/

home / user / www / functions /下的php文件。这些文件只有


所以,在index.php和其他文件中,我有

<?php

include( " ./ functions / function.create_html_page.inc\");

include(&.; functions / function.create_pdf.inc\"))

.... 。

?>


然后是函数文件,例如function.create_html_page.inc

<?php

函数create_html_page($ arg1,$ arg2){

...

}

?>


所以,我的php页面对函数文件有相对的引用,即

" ./ functions /"。


这样可行,但是当我想在一个函数中包含一个函数

文件时,我必须改变相对路径。所以,而不是

包括(./ functions / function.create_pdf.inc");

我应该有

包括(" ; ./ function.create_pdf.inc&););


理想情况下我想弄清楚绝对路径名,这样我就可以了b
不要必须更改相对路径名称,具体取决于

文件是在根目录或函数目录中,还是在任何其他
目录中。


换句话说,我可以做

<?php

$ path =" / home / user / www /" ;;

include($ path。functions / function.create_html_page.inc);

include($ path。functions / function.create_pdf.inc);

....

?>


然后include语句是相同的,不管是什么
他们所在的
目录。


但是,我希望解决方案更便携。在更改中我更改了

主机,或者将网站安装在另一台服务器上,我希望路径

名称不要硬编码,所以我没有改变任何东西。

类似


< ;?

$ path = get_base_directory();

include($ path。functions / function.create_html_page.inc);

include($ path。functions / function.create_pdf.inc);

....

?>


我的网站经过精心设计,以便子目录中的页面永远不会被提供给b $ b;用户始终在根目录中导航。所以,即使

如果函数子目录中的函数被调用,原始的

执行脚本也在根目录中。


我看了像basename这样的函数,但是他们希望这个目录为

一个参数。


我看了$ _SERVER和$ _ENV,他们有'' OLDPWD''

[OLDPWD] = / home / user / www

[PWD] = / home / user / www / functions


OLDPWD看起来会起作用,但我无法在任何地方找到它记录

,所以我不知道它是否可用作便携式

解决方案。如果它没有记录,我可能不会依赖它在大多数系统上的价格,对吧?


Anywho,我现在的想法是通过backtrace数组来查找原始脚本,从而得到基本目录的
。是否有更简单的方法来执行此操作?

Hello all -

I have a two part question.

First of all, I have a website under /home/user/www/. The index.php
and all the other website pages are under /home/user/www/. For
functions that are used in multiple files, I have php files under /
home/user/www/functions/. These files simply have

So, in index.php and other files, I have
<?php
include("./functions/function.create_html_page.inc");
include("./functions/function.create_pdf.inc");
.....
?>

And then function files, such as function.create_html_page.inc
<?php
function create_html_page( $arg1, $arg2 ) {
...
}
?>

So, my php pages have relative references to the function files, i.e.
"./functions/".

This works okay, but when I want to include a function in a function
file, I have to change the relative path. So, instead of
include("./functions/function.create_pdf.inc");
I should have
include("./function.create_pdf.inc");

Ideally I would like to figure out the absolute path name, so that I
don''t have to change the relative path names depending on whether the
file is in the root directory or the functions directory, or any other
directory for that matter.

In other words, I could do
<?php
$path = "/home/user/www/";
include( $path . "functions/function.create_html_page.inc");
include( $path . "functions/function.create_pdf.inc");
....
?>

And then the include statements are identical, no matter what
directory they''re in.

But, I would like the solution to be more portable. In change I change
hosts, or install the website on another server, I would like the path
name not to be hard-coded, so I wouldn''t have to change anything.
Something like

<?
$path = get_base_directory();
include( $path . "functions/function.create_html_page.inc");
include( $path . "functions/function.create_pdf.inc");
....
?>

My website is designed so that pages in subdirectories are never
served; the user is always navigating in the root directory. So, even
if a function in the functions subdirectory are called, the original
executing script was in the root.

I looked at functions like basename, but they expect the directory as
an argument.

I looked at $_SERVER and $_ENV, and they have ''OLDPWD''
[OLDPWD] =/home/user/www
[PWD] =/home/user/www/functions

OLDPWD looks like it would work, but I couldn''t find it documented
anywhere, so I don''t know if it would be useable as a portable
solution. If it''s not documented, I probably can''t rely on it being on
most systems, right?

Anywho, my thought now is to go through the backtrace array to figure
out the originating script, and thus the base directory. Is there an
easier way to do this?

推荐答案

arg1,


arg2){

...

}

?>


所以,我的php页面对函数文件有相对的引用,即

" ./ functions /"。


这样可行,但是当我想在函数中包含一个函数

文件,我必须改变相对路径。所以,而不是

包括(./ functions / function.create_pdf.inc");

我应该有

包括(" ; ./ function.create_pdf.inc&););


理想情况下我想弄清楚绝对路径名,这样我就可以了b
不要必须更改相对路径名称,具体取决于

文件是在根目录或函数目录中,还是在任何其他
目录中。


换句话说,我可以做

<?php
arg2 ) {
...
}
?>

So, my php pages have relative references to the function files, i.e.
"./functions/".

This works okay, but when I want to include a function in a function
file, I have to change the relative path. So, instead of
include("./functions/function.create_pdf.inc");
I should have
include("./function.create_pdf.inc");

Ideally I would like to figure out the absolute path name, so that I
don''t have to change the relative path names depending on whether the
file is in the root directory or the functions directory, or any other
directory for that matter.

In other words, I could do
<?php


path =" / home / user / www /" ;;

include(
path = "/home/user/www/";
include(


这篇关于得到绝对目录路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆