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.
Related
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.
Hey I am trying to include a file inside another file in PHP
If i write the entire path it does it with out problem.
$route = "/var/www/vhosts/aldroenergia.com/testmovil.aldroenergia.com/src/recursos/php/fEmail.php";
include($route);
but i would like to include with out writing the whole path.
Im including the fEmail.php inside
/var/www/vhosts/aldroenergia.com/testmovil.aldroenergia.com/src/ajax/correos/file.php
I've tried include("../../recursos/php/fEmail.php") but didnt work.
I've tried include(dirname(__FILE__."../../recursos/php/fEmail.php")); but failed too.
this is the folder structure.
--src
--recursos
--php
-fEmail.php
--ajax
--correos
-file.php
Constants paths and require statements are relative to the current file youre in.
To keep track of your paths I would suggest to use a central config file and define your root path in it:
# ./src/config.php
define("ROOT", __DIR__);
# Later include config.php and use:
require_once(ROOT . "/src/recursos/php/fEmail.php");
include(dirname(__FILE__."../../recursos/php/fEmail.php"));
when you are passing the result of __FILE__ concatenate to the ../../recursos/php/fEmail.php within the dirname function it will not work because It's a wrong path.
you must instead past just __FILE__ as parameter to dirname which will return the absolute path of the directory in which the file.php file is. after getting the contening directory path you can past the relative path to the fEmail.php file from file.php directory.
include(dirname(__FILE__) . "/../../recursos/php/fEmail.php");
Well, the structure of site is simple:
site.com
'config' folder
config.php
cesar.php
'login' folder
index.php
index.php
Config.php:
include_once '../config/cesar.php';
At site.com/index.php:
Warning: include_once(../config/cesar.php): failed to open stream: No such file or directory
At site.com/login/index.php everything is OK.
If I will remove one dot (./config/cesar.php), main index will become OK and login page will get the error.
How to make both codes work?
The .. in your path are used to go up a directory because of that the file won't exist where you are looking for it.
If you update it to be include_once 'config/cesar.php'; it should work since that will allow it to go down into the config directory rather than try to find a directory with the name of config 1 level above where index.php is located.
./ works since . is the notation for the current directory.
To answer your question, it wouldn't be possible to have the code work by using a relative path since both the files are in different locations on the server in relation to the one you want to include. If you want to have something that does then you will need to use an absolute path rather than the relative path. This would be something like /path/to/webdirectory/site.com/path/to/file/config.php (i.e. /home/charles/websites/site.com/config/config.php) in *nix and C:\path\to\webdirectory\site.com\path\to\file\config.php on windows.
In PHP you should be able to get the absolute path in a dynamic way by using the $_SERVER['DOCUMENT_ROOT'] variable. Ex: include_once $_SERVER['DOCUMENT_ROOT'] . '/config/cesar.php';
Warning: include_once(../config/cesar.php): failed to open stream: No such file or directory
I ran into a similar problem once. You need to remember that (./config/xxx.php) and (config/xxx.php) mean the same thing but (../config/xxx.php) will go up a directory and find the config folder and xxx.php file in it.
You can also use $base_url as a prepend string to all your paths to make it clean. Where:
$base_url = 'http://' . $_SERVER['SERVER_NAME'];
Then replace your paths like
../config/xxx.php
with
$base_url.'/config/xxx.php'
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.
Currently I am trying to include a PHP file from another directory.
public_html/a/class/test.php <-- from this file i would to include a file from
public_html/b/common.php <-- wanted to include this file
Not sure what I should do because I have tried using
dirname(__FILE__)
and this keeps on returning public_html/a/ for me instead.
I have tried something like this
dirname(__FILE__).'/../b/common.php'
but it does not help me in getting my file.
You can simply keep moving up the directory tree until you have the common ancestor:
require dirname(dirname(__DIR__)) . '/b/common.php';
The magic constant __DIR__ equals dirname(__FILE__), and was introduced in 5.3. Each use of dirname() goes back one directory, i.e.:
dirname('public_html/a/class'); // public_html/a
dirname('public_html/a'); // public_html
Btw, editors such as PhpStorm also understand this use of relative paths.
First of all i suggest you to define a variable for basepath and include that defined variable in relative files.
// This should be on any root directory file
define("PR_BASEPATH", dirname(__FILE__));
And according to your implementation, Assume you are in
public_html/a/class/test.php
and dirname(__FILE__) returns the directory name of the current file that always return the directory class according to test.php file.
And you want to include public_html/b/common.php that is on the other directory /b. So you have to get the document root directory first.
include $_SERVER['DOCUMENT_ROOT'] . "/b/common.php";
Take a look on $_SERVER['DOCUMENT_ROOT']
include('../../b/common.php');
would include file for you, make sure both directory have same usergroup as user.