网站根相对路径无效 [英] Site-root relative path doesn't work
问题描述
我不明白这一点.因为我使用的是php模板,所以整个网站搞砸了.
I'm not understanding this. It's screwing up whole site because I'm using a php template.
根据我已阅读的每篇文章,假定以'/'开头的链接使我从根开始.但是,当我以'/'开头时,它根本不起作用.
Supposedly beginning a link with '/' starts me at the root according to every article I've read. However, when I begin with '/' it doesn't work at all.
我的变量位于
public_html/cis130/textfiles/php/variables.php
我的template.php文件位于
My template.php file is in
public_html/cis130/textfiles/php/template.php
我的index.php文件位于
My index.php file is located at
public_html/cis130/index.php
它在我的index.php文件中读取,
In my index.php file it reads,
<?php
require('/cis130/textfiles/php/variables.php');
$currentpage = $page[0];
require('/cis130/textfiles/php/template.php');
?>
它不起作用...我得到致命错误:require():无法打开所需的'/cis130/final/textfiles/php/variables.php'(include_path ='.:/usr/local/php54/pear'),位于第3行的/home/philumpt/public_html/cis130/final/index.php中
It doesn't work... I get "Fatal error: require(): Failed opening required '/cis130/final/textfiles/php/variables.php' (include_path='.:/usr/local/php54/pear') in /home/philumpt/public_html/cis130/final/index.php on line 3"
如果我使用相对链接,
textfiles/php/file.php
然后它可以工作,但仅适用于index.php.我的其他页面位于"public_html/cis130/textfiles/pages/"中.如果那很奇怪,那是我班上的老师让我们做的.
then it works, but only for index.php. My other pages are located in 'public_html/cis130/textfiles/pages/'. If that's weird, it's what the teacher in my class is making us do.
推荐答案
只需将基本路径集用作配置,就不必担心相对路径,这样您就不会不断地在变相对位置.例如,在主配置文件中,您可以定义如下基本路径:
Stop worrying about relative path by just using a base path set as a configuration so you are not constantly juggling relative locations. For example, in your main config file, you can define a base path like this:
$BASE_PATH = '/the/path/to/the/codebase/';
如果您不知道文件的基本路径,请将此行放在PHP代码的顶部:
If you don’t know the base path to your files, then place this line at the top of your PHP code:
echo "Your path is: " . realpath(dirname(__FILE__)) . "<br />";
并加载该页面.顶部附近的某行应显示为:
And load that page. Somewhere near the top should be a line that reads:
您的路径是:/the/path/to/the/codebase/
Your path is: /the/path/to/the/codebase/
当然/the/path/to/the/codebase/
将是您的实际文件路径,但这将是您的基本路径.然后只需将 $ BASE_PATH
设置为该值.
Of course /the/path/to/the/codebase/
will be your actual file path, but that will be your base path. Then just set $BASE_PATH
to that value.
然后,当您执行 require
时,语法为:
Then when you do an require
, the syntax would be:
require($BASE_PATH . '/cis130/textfiles/php/variables.php');
这样做的好处是,无论代码库嵌套的深度如何,您始终将锚定在 $ BASE_PATH
的值上.不必担心相对路径问题,您的生活将变得更加轻松.
The benefit of this is no matter how deeply nested your codebase becomes, you will always be anchored to the value of $BASE_PATH
. And your life will be made tons easier thanks to not having to worry about relative path issues.
我还建议使用 require_once
而不是 require
以避免脚本可能无意间尝试多次加载同一文件的情况.
I would also recommend using require_once
instead of require
to avoid scenarios where your script might inadvertently attempt to load the same file more than once.
require_once($BASE_PATH . '/cis130/textfiles/php/variables.php');
这篇关于网站根相对路径无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!