PHP - Relative paths "require" - php

My directory structure looks like this:
blog -> admin -> index.php
blog.php
db.php
functions.php
I have been trying to include (require, really) blog.php in the admin/index.php, but facing lots of errors. I'm following a PHP course, and the instructor does the same thing successfully.
admin/index.php:
require "../blog.php";
which, in turn, requires two more files in its directory.
require "db.php";
require "functions.php";

If you find that relative include paths aren't working as expected, a quick fix is to prepend __DIR__ to the front of the path you're trying to include.
require __DIR__ . "/../blog.php";
It's reasonably clean, and you don't need to modify the include path or working directory.

You need to set the include_path in your php.ini.
If you want to set it at run-time, use set_include_path().

If you are including this files db.php and functions.php in index.php then you have to write this code
require "../db.php";
require "../functions.php";
OR if you are including this files in blog.php then write this code
require "db.php";
require "functions.php";

I like to start my files with chdir($_SERVER['DOCUMENT_ROOT']). This allows me to get a nice and logical base path for all my includes.

Related

PHP Require paths / Relative or absolute

New to PHP so trying to work out how relative and absolute paths work with require.
My structure looks like:
/index.php
/inc/db_functions.php
/inc/config.php
/login/login.php
db_functions.php includes config.php using
require_once 'inc/config.php';
index.php includes db_functions.php using require_once 'inc/db_functions.php';
If the user is not logged in then the browser redirects to login.php.
Originally login.php was in the root folder and used the same require once as index.php. It worked perfectly.
Now it is in its own subfolder.
I changed the require_once to use
require_once('../inc/db_connect.php');
The code worked until it got to the require_once line in db_connect.php and then failed.
I changed the require once in login.php to be:
require_once(__DIR__.'/../inc/db_connect.php');
Again it failed when it got to the require_once in db_connect.php
I changed the require_once in db_connect to be an absolute path. Again it failed.
Is there any way to deal with this as it seems that the include paths for an included file are changed by the location of the calling file ( which I guess makes sense) but how to make it more robust.
db_functions.php must include config.php using
require_once 'config.php'; because they are in same folder.
In general, best would be to use full paths.

How to call require files from public_html to wordpress theme directory

I want to call two require files from public_html to WordPress theme directory,
Example - require 'includes/functions.php';
require 'init.php';
those files are located in public_html and i am calling from public_html/wp-content/themes/arkahost/header.php
I modified require condition, but still no luck. anyone have an idea behind it?
Take the whole path to include the files.
For example ,
require( '/home/username/public_html/include-shared.php' );
The best way to do this is:
The best way is: require $_SERVER['DOCUMENT_ROOT'] . 'somefilename.php';

PHP require, dot as prefix

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).

How to properly include or require a file in an another folder from a file in an another folder

For example, I am going to require index.php (file found in config folder) in index.php which is in the anotherfolder. I am just doing this require dirname(__dir__).'config/index.php'; what I understand in this line is that it will get my working directory(blog1) and find the folder named config and see if there's an index.php in it. This would get the job done but I'm not sure if I am doing it right, and is my understanding is correct?
for example this is my directory:
blog1
config
index.php
anotherfolder
index.php
If anyone can help me with my little problem that would be great, (don't mind the - sign I don't know how to else illustrate a directory.
If you are in anotherfolder and want to include index.php from config then use this code:
require __DIR__ . '/config/index.php';
P.S It is a good practice to define a common set of constants like BASE_URL and BASE_PATH in a configuration file and then include files with absolute addressing by using BASE_PATH e.g.
require BASE_PATH . '/file_name.php';
You should also make a habbit of that.

PHP Relative paths for require file

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__).

Categories