Here is my file structure on my cpanel webserver
root
> vendor
> autoload.php
> public_html
> folder
> file2.php
> script_folder
> include_file.php
> file1.php
Inside include_file.php I have
require_once('../vendor/autoload.php');
file1.php and file2.php both contain the same call to include_file.php
require_once($_SERVER["DOCUMENT_ROOT"]."/script_folder/include_file.php");
This works fine when I run file1.php but when I run file2.php i receive the following error message.
No such file or directory Fatal error: require_once(): Failed opening required ‘../vendor/autoload.php’ (include_path=’.:/opt/cpanel/ea-php71/root/usr/share/pear’) in /lvl1/lvl2/public_html/script_folder/include_file.php
However, if I change require_once('../vendor/autoload.php');
to require_once('../../vendor/autoload.php');
in include_file.php then file2.php works and file1.php does not work. It showes a similar error.
I understand this is a file path issue but, what I don’t understand is why. Shouldn’t the path in include_file.php
always be the same no matter what file is calling it; i.e. file1.php or file2.php?
The way I see it is the the actual require_once statement is being called from include_file.php but, the behavior I’m seeing makes me think the require_once statement is being ran from file1.php or file2.php resulting in the filepath error.
Can someone please clarify?
UPDATE: inside include_file.php I have tried using:
require_once($_SERVER["DOCUMENT_ROOT"] . '/vendor/autoload.php');
and
require_once(dirname(__FILE__).'/vendor/autoload.php');
neither of these work since both return public_html
as the main working directory. My vendor
folder is outside the main working directory.
what is returned is this:
/script_folder/vendor/autoload.php
I understand I can simply include the correct file path at the beginning of file1.php
and file2.php
but, I was trying to figure out a way to reduce the number of requires I need in each file to only one by pointing them to include_file.php
, then letting include_file.php
do the rest of the work.
My include_file.php
file contains several other require statements to other scripts on my server. Kind of like a mini autoloader. All of the other require statements work fine except the autoload.php
one I’m having trouble with here.
The only difference I can see is my other scripts are within my public_html
folder and my autoload.php
file is located outside of public_html
.
2
Answers
This seems to work perfectly. Guess instead of a relative path I need to use the absolute path.
include_file.php:
it allows me to call:
require_once($_SERVER["DOCUMENT_ROOT"]."/script_folder/include_file.php");
from any files that are in my public_html folder.
in include_file.php
$_SERVER["DOCUMENT_ROOT"]
returns/lvl1/lvl2/public_html
(the full server path to the publicly visible folder)then I replace
public_html
with the the path to my autoloadervendor/autoload.php
Assuming you are using Composer, you only need to require “autoload.php” once in
file1.php
and the file should contain a namespace likenamespace App;
at the top of the file below the opening PHP tag. Subsequently,file2.php
should containnamespace Appfolder;
andincluded_file.php
should containnamespace Appscript_folder;
.The namespace should also be defined in
composer.json
like so: