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, a
.htaccess
does not help you. It is an Apache configuration and it does not affect the behavior of PHP in any way.
The code:
require "/assets/includes/scriptname.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.
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.
Given the sample file structure:
public_html
|
+- index.php
|
+- assets
| |
| +- somescript.php
|
+- includes
|
+- header.php
|
+- footer.php
Let's say you need to include assets/somescript.php
in index.php
. Write this in index.php
:
require __DIR__.'/assets/somescript.php';
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'
.
If somescript.php
needs to include header.php
it should do it like this:
require dirname(__DIR__).'/includes/header.php';
and so on.
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.