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.
Assuming you are using Composer, you only need to require "autoload.php" once in file1.php and the file should contain a namespace like namespace App; at the top of the file below the opening PHP tag. Subsequently, file2.php should contain namespace App\folder; and included_file.php should contain namespace App\script_folder;.
The namespace should also be defined in composer.json like so:
"autoload": {
"psr-4": {
"App\\": "public_html/"
}
}
This seems to work perfectly. Guess instead of a relative path I need to use the absolute path.
include_file.php:
$autoload = str_replace('public_html','vendor/autoload.php',$_SERVER["DOCUMENT_ROOT"]);
include($autoload);
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 autoloader vendor/autoload.php
Related
What is the difference between these two in PHP?
require "./vendor/autoload.php";
vs
require "vendor/autoload.php";
For both statements the autoload.php script is found, but in certain environment the autoloader itself does not find classes. I'm not trying to solve the autoloader problem itself, but try to understand why these two make it behave differently.
The . refers to the folder that you are in, it's most a unix syntax for files them for the php. I think you should use __DIR__ to prefix the included files, so you can avoid some problems with relative paths
The . gives you the ability to set the path of the included files relatively to the path of the original file that run (the file that included them).
Lets take the following structure:
/index.php
/file2.php
/folder/
/file1.php
If index.php includes file1.php, and you want file1.php to include file2.php - you can do this using require './file2.php'; (inside file1.php, which is in the inner folder).
If you use require 'file2.php'; inside file1.php you are looking for file2.php inside the folder (which will give you an error, because the file is not there).
I've been going over those two topics:
include, require and relative paths
PHP - with require_once/include/require, the path is relative to what?
and couldn't make my script to work, none of presented methods are working or maybe I'm doing something wrong.
Anyway this is where my problem occurred:
Root/ //this is root location for server
APP/ //this is root location for script
Root/APP/core/init.php //this is where I include classes and functions from
Root/APP/classes/some_class.php //this is where all classes are
Root/APP/functions/some_function.php //this is where all functions are
and so obviously I need to include init.php everywhere so I did in every file like this:
require_once 'core/init.php';
it was working until I have decided to create a location for admin files like this:
Root/APP/Admin/some_admin_file.php
and when I included init this way:
require_once '../core/init.php';
script failed to open functions, no such file in APP/Core/ folder
so I used DIR method presented in topic above and than even weirder thing happened, error:
no such file in APP/Core/classes/Admin/
What is that? :D I'm lost with this, could someone help a bit ;)
Include paths are relative to the current working directory, which can be inspected using getcwd(); this can be a source of many issues when your project becomes bigger.
To make include paths more stable, you should use the __DIR__ and __FILE__ magic constants; for instance, in your particular case:
require_once dirname(__DIR__) . '/core/init.php';
The dirname(__DIR__) expression is effectively the parent directory of the script that's currently being run.
Btw, __DIR__ could also be written as dirname(__FILE__).
I have two files in the same folder, let's call them file1 and file2. Each of them use the same path to an include file called dbgeneral.php. They each then call the function admin_page_init() to check session information and bar access to non admins. One of the files works fine, however the other throws up an error on the require. If I copy paste the included file into the file which throws the error it works fine. If I include a copy of the file from a different folder it works fine. For some reason it refuses to find the file if it is in the same folder. I can use file_exists() and it returns positive. As far as I know there is no reason for this behaviour.
Folder Structure:
html/
/ dbgeneral.php
/ file1.php
/ file2.php
code:
require_once 'dbgeneral.php';
admin_page_init();
Also note that I tried using the superglobal $_SERVER to acquire the real absolute path. Also, this works on my local test server, but does not work on my web host.
include_once 'dbgeneral.php';
include_once 'file1.php';
include_once 'file2.php';
This will work. If it does not, it will show you an error as to why it did not work; require will not show you an error in the same fashion.
I have a home.php file which references fileA using php require command which is in the same folder i.e. localhost. the code is:
<?php // /localhost/home.php
require ('/fileA.php');
?>
fileA references another file in its code fileB.
so far its workin properly
when i access fileC.php
i reference fileA using
<?php // /localhost/A/B/fileC.php
require ('../../fileA.php');
?>
the code is able to acquire fileA but only works properly when fileB is in the same folder as fileC else produces a 404 error.I am not able to understand why is this happening since fileB is called by fileA.
thanks.
When using relative file paths, keep in mind that they are relative to your current working directory, not to file that performs inclusion. current working directory is set automatically on the first request (A/B in your case).
Some simple solutions: always use dirname(__FILE__) before including file, this way it be relative to the file the is in:
<?php
require(dirname(__FILE__)."/FileA.php");
require(dirname(__FILE__)."/../../FileB.php");
I have a directory root:
index.php
includes/
template.php
testfile.php
phpFiles/
processInput.php
testfile.php
index.php:
require_once("includes/template.php");
template.php:
require_once("includes/phpFiles/processInput.php")
processInput.php:
require_once("testfile.php")
require_once("../testfile.php")
This code will work when you run index.php, of course it will not work when you run template.php.
As you can see, index.php includes template.php like normal. But in template.php, you have to include like if you are in the directory that index.php is in. But then, in processInput.php, you include as if you are in the directory that processInput.php is in.
Why is this happening, and how can I fix it so that the include path is always the directory of the file that the require is done in? The second included file have the same include path as the requested file, but the next one does not.
Thanks for your help!
EDIT: The strange thing is that I've included classes in a class folder. And it included other files as it is supposed to, even though the paths are relative. WHY does this happen, and how can I fix it?
VERY IMPORTANT EDIT: I just realized that all this is because in my example, the inclusion in includes/phpFiles/processInput.php includes a file in the same directory: require_once("file in same dir.php"); This is the reason. If you are including a file with out specifying anything more than the filename, the include_path is actually the dir where the file the require is written in is in. Can anyone confirm this?
Use an absolute path.
require_once($_SERVER['DOCUMENT_ROOT']."/includes/phpFiles/processInput.php");
Use a similar form for all your required files and they will work no matter where you are.
You can do this in a few ways, amongst others:
Use set_include_path to control the directories from where to perform require() calls.
Define a common absolute base path in a constant that you define in index.php and use that in every require() statement (e.g. require(BASEPATH . '/includes/template.php')).
Use relative paths everywhere and leverage dirname(__FILE__) or __DIR__ to turn them into absolute paths. For instance: require(__DIR__ . '/phpFiles/processInput.php');
By default, the current working directory is used in the include path; you can verify this by inspecting the output of get_include_path(). However, this is not relative to where the include() is made from; it's relative to the main executing script.
You're using relative paths. You need to use absolute paths: $_SERVER['DOCUMENT_ROOT'].
When you include/require, you are basically temporarily moving all code from one file, to another.
so if file1.php (which is located in root) contains:
require("folder/file.php");
and you include file1.php in file2.php (which is in a different location (say folder directory for example):
file2.php:
require("../file1.php");
Now all of file1.php code is in file2.php. So file2.php will look like this:
require("../file1.php");
require("folder/file.php");//but because file2.php is already in the `folder` directory, this path does not exist...
index.php:
require_once("includes/template.php");
template.php:
require_once("includes/phpFiles/processInput.php")
Your directory structure is off. The file inclusion is being seen from the file you're using it from. So, "template.php" is looking for an "includes/" folder in its current folder (/includes/).
As others are saying, use absolute paths, which will make sure you're always going at it from the file system root, or use:
require_once("phpFiles/processInput.php")
In your template.php file (which is far more likely to break if you ever move things around, which is why others all recommend using absolute paths from the file system root).
BTW, if you're using "index.php" as some kind of framework system, you can consider defining a variable that stores the address of common files such as:
define('APPLICATION_PATH', realpath(dirname(__FILE__));
define('PHPFILES_PATH', APPLICAITON_PATH . '/includes/phpFiles/');