Get the root folder path in php using include - php

I have php web application named mywebsite inside c:\wamp\www\ .
This folder consist of featured/fashionWeek/database/Mysql.php file. I use dirname(FILE) function to get path of folder from root so that I can place the folder to any place in public_html folder of live server.
It works fine in my localhost. I have following code inside wamp\www\mywebsite\featured\fashionWeek\database\Mysql.php The include(dirname(FILE); works fine that returns this path: C:/wamp/www/mywebsite/featured/fashionWeek/database
<?php include(dirname(__FILE__).'/../config/Dbconfig.php') ;?>
But When I placed the mywebsite folder under public_html folder of the live server. It gives following errors:
Warning: include(/home/websitename/public_html/featured/fashionWeek/database/../config/Dbconfig.php) [function.include]: failed to open stream: No such file or directory in /home/websitename/public_html/featured/fashionWeek/database/Mysql.php on line 3
Fatal error: include() [function.include]: Failed opening required '/home/websitename/public_html/featured/fashionWeek/database/../config/Dbconfig.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/websitename/public_html/featured/fashionWeek/database/Mysql.php on line 3

To go backwards, i.e '..', you use the dirname function.
<?php
include(dirname(dirname(__FILE__)).'/config/Dbconfig.php');
Should work.
More
It will be tiresome to manage these eventually, so I suggest in your original file, you define a ROOT constant. That will contain the absolute path to the root of your project. It's simple enough.
defined('ROOT') or define('ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR);

https://stackoverflow.com/a/2152320/1528331
See if that helps. Also,
dirname(__FILE__) is the same as __DIR__

a Little Bit Flabby , but it may help you
if(!function_exists('getRootdir'))
{
function getRootdir($NFILE)
{
$filefitter="";
while(!file_exists($filefitter.$NFILE))
$filefitter="../".$filefitter;
return $filefitter;
}
}
use
like $rootpath=getRootdir("Root identifier folder / php file");

Related

Cannot find a file available in the same folder

I'm trying to read the content of config.json file which is available in the same folder of the file that read it, this file is called settings.php, I actually have this line of code in settings.php:
$jsonData = file('config.json');
the problem's that I get:
Warning: file(config.json): failed to open stream: No such file or directory
this is weird because as you can see from the image:
I'm trying this app on localhost with mamp
This is most likely because your settings.php is included by a index.php that's a directory higher.
Php file opening will always assume from the "parent" php file where execution started as base directory and not of the included files.
Try using __DIR__
$jsonData = file(__DIR__.'/config.json');
__DIR__ The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(FILE). This directory name does not have a trailing slash unless it is the root directory.

Warning: include_once(../settings/settings.php): failed to open stream - but the path is actually right?

Warning: include_once(../settings/settings.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/Project/classes/user.class.php on line 2
Warning: include_once(): Failed opening '../settings/settings.php' for inclusion (include_path='.:/Applications/MAMP/bin/php/php7.2.1/lib/php') in /Applications/MAMP/htdocs/Snapshot/classes/user.class.php on line 2
But the file location is actually right? It even gives me auto path fill suggestions (visual code plugin) to that path!
Why does this happen is this something to do with the / or the second warning line?
So I'm trying to include the settings.php file from inside the User.class.php file with the following code.
include_once("../settings/settings.php");
../ -> goes back into the root folder
settings/ -> enters settings folder
settings.php -> should locate the needed .php file
Replace
include_once("../settings/settings.php");
with
include_once(__DIR__ . "/../settings/settings.php");
__DIR__ will resolve in the absolute path of the file where this constant is used. You can navigate your folders from that.
Use an absolute path to the file. You can use $_SERVER['DOCUMENT_ROOT'] or create a const variable BASEPATH that you can easily set root directory value to.
include_once($_SERVER['DOCUMENT_ROOT']."/settings/settings.php");
OR
define('BASEPATH', "/Applications/MAMP/htdocs");
include_once(BASEPATH."/settings/settings.php");
Depending on where you call your class. Better yet is to use configuration file to fulfil the require/include path like every PHP framework does.

PHP rquire_once from root

Firs of im pretty new in PHP.
I want to include a php file from root in another php file like this.
require_once "/getPublisher.php?id=' . $xy;
The file where i type this code is in '/page/page.php'.
--------------- ROOT -----------------
-page
-> page.php----------------
- trying to require_once '/getPublisher.php'
- into '/page/page.php'
-getPublisher.php-------------
I am getting these error codes back.
Warning: require_once(/users/uplade/www/getPublisher.php?id=123)
[function.require-once]: failed to open stream: No such file or
directory in /users/uplade/www/page/page.php on line 83
and
Fatal error: require_once() [function.require]: Failed opening
required '/users/uplade/www/getPublisher.php?id=123'
(include_path='.') in /users/uplade/www/page/page.php on
line 83
So how to include root files in non-root files?
EDIT:
Warning: require_once(/getPublisher.php) [function.require-once]: failed to open stream: No such file or directory in /users/uplade/www/page/page.php on line 84
WHAT IM DOING WRONG.
SOLUTION:
I removed the slash require_once 'getPublisher.php'; Thats way it worked for me!
You can't use parameters on require(). Try removing it:
require_once "/getPublisher.php";
With require_once "/getPublisher.php"; you are trying to include a file from the filesystem root, not from the document root.
If the files would be in the same directory, you would include it like this:
require_once "getPublisher.php";
But in your case, it's one level up, so just include it like this:
require_once "../getPublisher.php";
reuire and include (and their _once variants) locate a file on the filesystem based on its absolute path, falling back to a configurable set of include paths (used for placing shared libraries in standard locations).
There are two main ways to reference a file reliably when you don't know exactly where on the file system it will be (e.g. because you're going to install the software into different locations on different servers):
Relative to the "document root", that is the path configured in the web server to be the location of the URL "/". This directory is available as the superglobal variable $_SERVER['DOCUMENT_ROOT']; e.g. require $_SERVER['DOCUMENT_ROOT'] . '/getPublisher.php';
Relative to the current file, that is the file you are writing the PHP code in (regardless of how that file was accessed). This directory is available as the magic constant __DIR__; e.g. require __DIR__ . '/../getPublisher.php';
Notice in the second example I had to "climb up" from the current directory using .. which means "parent directory"; you can climb as high as you like with this: '/../../../../' etc

Base Directory Change Issue

In my app, I'm using an AJAX call to retrieve information, which requires the inclusion of a db_connect.php file. db_connect.php requires other files for it to work.
If I include db_connect.php from the AJAX file, naturally, errors are returned from includes within the db_connect.php file because db_connect.php's includes are relative to the base directory of my application.
How can I change the working directory to the base directory of the application, so the included files will function properly?
What I've tried is using the chdir function:
echo getcwd() . "<br/>";
chdir("../../");
echo getcwd();
This correctly outputs:
/my/webserver/app/lib/ajax
/my/webserver/app
However, the include errors act like I haven't just changed the directory:
Warning: include_once(functions/clean_string.func.php) [function.include-once]: failed to open stream: No such file or directory in my/webserver/app/lib/ajax/ajax.php on line 8
What am I doing wrong?
Then use absolute paths.
For example in a config file, that resides in base of the app, define a base path that is absolute.
define("BASE_PATH", dirname(__FILE__)); //This now is a ABSOLUTE path to a dir that this file is in.
Then use:
include BASE_PATH . "/dir/dir/file.php";
Then, you just have to include the config file in every script at then top relatively from where you are in the app and then just use BASE_PATH in all other includes.
getcwd() does not return your include path; instead, try something like this:
chdir("../../");
include(getcwd()."/functions/clean_string.func.php");

Why do my php includes return, "failed to open stream: No such file or directory in..."

I'm using simple include files for common elements on a mostly static website. I do so like this
<?php include('/inc/header.php'); ?>
But this doesn't work unless i remove the leading slash, and then it only works on pages in the root directory. What am I doing wrong?
'/' means the real server root directory.
If you need web document root use :
include("{$_SERVER['DOCUMENT_ROOT']}/inc/header.php");
/... is an absolute path on unix systems. To specify a relative path use ./.... That will be relative to the called file's directory.
You have an absolute path - i.e. starts with /. So it is looking in your server root.
Without the slash it is a relative path and will look relative to the PHP file's path.
Use this solution
I also facing same problem
Warning: include(/test/assets/header.php) [function.include]: failed to open stream: No such file or directory
Warning: include() [function.include]: Failed opening '/test/assets/header.php' for inclusion (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear')
But I solve them after searching it on google in 2 dyas.
Solution:
you have to define your path then use include function as blow:
define (DOC_ROOT_PATH, $_SERVER['DOCUMENT_ROOT'].'/');
you can use any directory structure after that.
add "."before path like that :
<?php include('./inc/header.php'); ?>
The path meanings:
/ is the root of the current drive.
./ is the current directory (that what you want).
../ is the parent of the current directory.

Categories