問題描述
我使用的是 000webhost,它使用根文件夾中的 public_html 文件夾作為站點的可見根.在那個文件夾中,我有一個包含一些 PHP 腳本的資產(chǎn)文件夾,以及包含 PHP 索引頁面的其他文件夾.使用 require "/assets/includes/scriptname.php";
不起作用,因為它試圖找到 public_html 的同級文件夾.我可以編輯 .htaccess 以更改相對于 PHP 查找的根文件夾,但我不知道如何操作.
I'm using 000webhost, which uses a public_html folder in the root folder as the visible root for the site. In that folder, I have an assets folder with some PHP scripts, and other folders with PHP index pages. Using require "/assets/includes/scriptname.php";
does not work, as it tries to find a sibling folder to public_html. I'm allowed to edit .htaccess to change the root folder relative to the PHP lookup, but I don't know how.
文件樹:
public_html (within root, but simulated root)
folder1
index.php
folder2
index.php
folder3
index.php
assets
subfolder
subfolder
index.php
簡而言之,如何在 /public_html/
中創(chuàng)建提到的代碼點而不顯式聲明它(最好作為 .htaccess 更改,因為我希望我的代碼能夠移動到無需重寫任何內(nèi)容的不同主機).
In short, how do I make the mentioned code point inside /public_html/
without explicitly declaring it (preferably as a .htaccess change, as I want my code to be able to be moved to a different host without rewriting anything).
對于 .htaccess 重寫的答案,您能解釋一下它的每一行是如何工作的嗎?謝謝.
For an answer with the .htaccess rewrite, could you explain how each line of it works? Thanks.
推薦答案
.htaccess
對您沒有幫助.它是一個 Apache 配置,它不會以任何方式影響 PHP 的行為.
.htaccess
does not help you. It is an Apache configuration and it does not affect the behavior of PHP in any way.
代碼:
require "/assets/includes/scriptname.php"
告訴 PHP 使用絕對路徑包含文件.不推薦使用硬編碼的絕對路徑,因為當您將其移動到另一個目錄或具有不同路徑的不同服務器時,代碼將不起作用.
tells PHP to include a file using an absolute path. Using hardcoded absolute paths is not recommended because the code won't work when you move it into another directory or on a different server that has different paths.
指定包含文件路徑的最佳方法是在運行時生成它,從包含程序的路徑開始.PHP 函數(shù) dirname()
和常量 __DIR__
是這里的幫手.
The best way to specify the path of an included file is to generate it runtime, starting from the path of the includer. The PHP function dirname()
and the constant __DIR__
are the helpers here.
給定示例文件結構:
public_html
|
+- index.php
|
+- assets
| |
| +- somescript.php
|
+- includes
|
+- header.php
|
+- footer.php
假設您需要在 index.php
中包含 assets/somescript.php
.在 index.php
中寫入:
Let's say you need to include assets/somescript.php
in index.php
. Write this in index.php
:
require __DIR__.'/assets/somescript.php';
魔術常量 __DIR__
包含使用它的文件的目錄.對于 index.php
,__DIR__
設置為 '/(...path-to-your-user-directory...)/public_html'代碼>.
The magic constant __DIR__
contains the directory of the file where it is used. For index.php
, __DIR__
is set to '/(...path-to-your-user-directory...)/public_html'
.
如果 somescript.php
需要包含 header.php
它應該這樣做:
If somescript.php
needs to include header.php
it should do it like this:
require dirname(__DIR__).'/includes/header.php';
等等.
通過這種方式,您可以將整個應用程序移動到不同的目錄、不同的服務器甚至不同的操作系統(tǒng)上,它仍然可以工作.
This way you can move the entire application to a different directory, on a different server and even on a different OS and it will still work.
這篇關于帶有 .htaccess 的 PHP 根目錄的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!