Include files from parent directory - php

I'm facing a problem using relative path...I have that directory structure
-dev
--vendor
-autoload.php
--includes
-index.php
now i need to include autoload.php in index.php
As adviced on previous answers i tried include('../vendor/autoload.php'); but it didnt work.
but this didnt work so i had to use this walkaround require_once($_SERVER['DOCUMENT_ROOT'] . '/dev/vendor/autoload.php');
So im wondering why the relative path didnt work and how to make it work?

The only thing that comes to my mind right now is the current working directory.
If you don't specify an absolute path the current working directory will be chosen.
E.g.
<?php
chdir("/tmp");
include "test.php"; // will include /tmp/test.php
And
<?php
chdir("/home/marco");
include "test.php"; // will include /home/marco/test.php
Try changing your path to: __DIR__."/../vendor/autoload.php".
See PHP: Magic Constants for further reference.

Related

Issues with relative/absolute path

I've searched ways to do it so it would work but none of them seemed to work for all paths, so I was wondering if you could give me a direction on how to do this:
This is the structure:
Home Directoy
config.php
index.php
includes/
user_login.php
applicant_track.php
ucp/
index.php
When I am trying to include includes/user_login.php in ucp/index.php it doesn't let me and says that it cannot find the file, my code is in ucp/index.php is:
if(!defined('root_path'))
{
define('root_path', realpath(dirname(__FILE__)));
}
include(root_path . '\config.php');
switch($_GET["a"])
{
case null: include(root_path . '\index.php'); break;
case "login": include(root_path . '\includes\user_login.php'); break;
}
This is what I get:
Warning: include(E:\xampp\htdocs\aod_panel\ucp\config.php): failed to open stream:
I'd be happy for an advise on how to fix this.
Use following path
define('root_path', realpath(dirname(dirname(__FILE__))));
instead of your code for defining real path. As your folder structure is like that.
See your index.php is in ucp folder but you want path of config.php. So go back one directory and get config.php path.
Your root path does not points to the actual root of your project. Your actual root is someLocation/yourProject.
the root you have defined is someLocation/yourProject/includes/
Then you want to include file in another folder. Hence it cannot find it. Define the root of your path to your actual project root and not inside includes directory.
To do this, you can define the root path in your config file and read it from there.
This is most likely one of your other scripts includes the config.php without proper path. Set up your include paths using the following code
set_include_path(get_include_path() . PATH_SEPARATOR . $root_path);
Also change the include to require_once to prevent multiple includes.

PHP - Going back directories

I've encountered something when trying to include/require a php script that's 2 directories back. It's not really a problem as I figured out a work around, however I'd love an explanation for what's happening.
Here's the file structure:
appCode
db.php (File I'm trying to include)
studentManagement
index.php
dep
getData.php (File I'm trying to include db.php into)
I want to include appCode/db.php in studentManagement/dep/getData.php.
getdata.php is executed with ajax from index.php
When I use:
require_once("../../appCode/db.php");
It doesn't work.
The only way it works is it I change directory first:
chdir("../");
require_once("../appCode/db.php");
Why won't the first method work? I've also tried using include instead of require but it's the same. I'm testing it on mamp 3.0.4.
Any help appreciated!!
that is because when you require(),include() and their variants it's always relative to the initial php file called (in your case index.php)
in fact chdir has nothing to do with it, and this:
require_once("../appCode/db.php");
is the right way to call it.
always place your mental self (!) as if you were index.php when you require files and work with directories. your "active directory" is the one where index.php is placed. you can always verify this current working directory with getcwd() http://php.net/manual/en/function.getcwd.php
If you know your entire directory all the way from root you can use:
http://php.net/manual/en/function.set-include-path.php
Now instead of using relative paths you can always include or require files starting at your include path.
You could put this in getData.php:
set_include_path ('/homepages/www/2/yourworkingpath');//Use your own directory
require_once 'appCode/db.php';
You could also do the same thing in your other files if you need to include and it will always use your include path so you don't have to keep figuring out which directory to change to. Hopefully this helps a bit.

File path error with forward slash at beginning of path

I have a folder with a bunch of articles that all use the same header and footer, which are in an includes/ folder. I also needed to add another file that is not an article and I therefore do not want it in the folder with the other articles, but instead in the includes/ folder. I still want it to use the same header and footer as the articles though, so naturally I just use a command like
include 'article_header.php';
The error is inside the header, which has include commands inside of it. Because the article_header.php file is built for the articles, the include commands inside of it look like the following:
<?php
include 'includes/article_social_container.php';
include 'includes/article_search_container.php';
include 'includes/membership_container.php';
?>
Since this obviously is not going to work for the file inside the includes/ folder. So I tried using a forward slash and starting from the root directory so it works for any file that uses it.
<?php
include '/root/folder/includes/article_social_container.php';
//other code
?>
However, this does not seem to work. I have had trouble with the forward slash at the beginning of a file path in the past, but it has also worked for me other times.
Also, why wouldn't someone always use a forward slash and start at the root directory, just to keep things safe? It probably is the answer to this question because it seems totally sensible to me unless it was for a similar purpose of the open_basedir() function in php.
Thanks a lot for any help.
Take a look at the __DIR__ magic constant. It resolves to the directory of the script in which it appears. Using this, you only need to use relative paths, for example...
// within the "includes" directory
include __DIR__ . '/article_social_container.php';
Another option is to configure your application's include_path. Say you have a script bootstrap.php in your includes directory with the following...
set_include_path(implode(PATH_SEPARATOR, [
__DIR__, // the "includes" directory
get_include_path()
]));
This will add the includes directory to the top of your include_path stack. You can then simply do the following from any other script...
require_once __DIR__ . '/relative/path/to/includes/bootstrap.php';
include 'article_header.php';
Any included scripts from this point on will have the same include_path configuration so they in turn can simply use...
include 'article_social_container.php';
include 'article_search_container.php';
// etc

include and include_once issue in PHP

I currently working on a PHP project. I copy the project file to my local box. It runs fine except one thing.
Here is the folder hierarchy:
root/index.php
root/event/admin/list.php
root/event/admin/functions.php
In the index.php, there is a line:
<?php include ("event/admin/list.php"); ?>
which should include the list.php
However in the list.php, there is a line:
<?php include_once "event/admin/functions.php";?>
Since the list.php is not in the root directory, event/admin/functions.php did not get call and my local index.php fail to load this part.
But the production is working fine.
Does anyone know what happened? Is that a way to setup include/include_once always use ROOT directory without using something like $_SERVER["DOCUMENT_ROOT"]? Thanks a lot.
It is a good idea to use $_SERVER['DOCUMENT_ROOT'], in my opinion. You can do so like this:
include_once($_SERVER['DOCUMENT_ROOT'] . "/path/to/file.php");
However, try replacing he code in list.php with this:
<?php include_once "../../event/admin/functions.php";?>
This is a known issue relating to relative paths. Thus, DOCUMENT_ROOT is preferable. Alternatively, you can edit include_path.
Set the include path to whatever is useful for your probject.
This is a common issue related to relative paths. You can either include your files from an absolute path, or modify your include_path in php.ini to use your doc root, and specify files relative to there.
Compare the include path configuration between the two machines.

php question include an included file

I got a problem about including a included file in PHP.
Project
functions(folder) has a.php
xml(folder) has b.xml
index.php
This is my project structure(sorry about that, I can't post images).
I try to use "index.php" to include "a.php" while "a.php" is using "b.xml"
this is what i did on XAMPP and it works perfectly:
in index.php I wrote: include 'functions/a.php';
in a.php I wrote: $xml->load('xml/b.xml');
However if I copy these to my Uni's apache server, it can't open this b.xml.
This is not permission because when i change to absolute path it works...
Thank you guys in advance:-)
in a.php you should refer to ../xml/b.xml if you use include
thing is, it depeneds on when $xml->load() is defined. if it's your own code then put the path relative to the definition. otherwise "../xml/b.xml" should work.
you can always to $_SERVER['DOCUMENT_ROOT'], but i myself like defining directories as constants (with absolute path) and using them around the project.
define('DIR_ROOT', $_SERVER['DOCUMENT_ROOT'] . '/');
define('DIR_FUNCTIONS', DIR_ROOT . 'functions/');
define('DIR_XML', DIR_ROOT . 'xml/');
Try using set_include_path() to set the include path to your application's root directory; then you should be able to include files relative to this path.
It's always better to use absolute paths, even if you have to construct it (e.g. $XML_PATH = $PATH_TO_BASE . 'xml/b.xml'; )
If you can't do that, you should add xml's parent to your path.

Categories