问题
So, i am working on an eCommerce style of website and i own a host/domain. From my understanding, i should place my PHP files in another location than public_html(for security purposes), the location of index.html being in /home/user/public_html. I want to put my PHP files in the php folder. I have tried using:
include("home/user/php/file.php")
or
include("../php/file.php").
I have enabled the use of these commands in the ini file and i also tried to set permissions to the folders(read/write/execute).One thing that was working is to set a subdomain with its "root" folder to the php folder, but i guess that defeats the purpose of having the files somewhere else on the server, especially because i was using an object to place my php like so:
<object class="php-script" data="database.php"></object>
and under my java scripts i put :
<?php
include("/home/user/php/database.php");
?>
Thanks in advance for helping me.
回答1:
Normally using relative paths with include or require means using relative paths starting from the directory your first called script of an URL call is located.
Example:
application
src
database.php
somescript.php
public (document root)
index.php
news.php
http://example.com/index.php
public/index.php
<?php
include '../src/database.php'
This should be easy to understand.
http://example.com/news.php
public/news.php
<?php
include '../src/somescript.php';
src/somescript.php
<?php
include '../src/database.php';
So take care of the include in somescript.php. It doesn't matter that it is in the src folder. An include like include 'database.php' will fail, because it's the path of the news.php which matters.
Edit (2018-04-04)
I forgot to mention. Consider to use require() / require_once() instead of include() / include_once().
Difference between require, include, require_once and include_once?
回答2:
Just try the following code. see what it outputs. basically u need to use: $_SERVER['DOCUMENT_ROOT'] to navigate to file instead typing path yourself.
//to get site url
echo $site_url = "http://" . $_SERVER['SERVER_NAME'] . dirname($_SERVER['PHP_SELF']);
//to the folder
echo $dir = $_SERVER['DOCUMENT_ROOT'].'/user/php/';
if (file_exists($dir) == false) {
echo "Directory $dir not found!";
} else {
echo "directory found and we are ready to proceed.";
}
let me know your response, i ll help u further.
来源:https://stackoverflow.com/questions/49631265/using-the-include-function-in-php