File linkage in PHP - php

I need some help in linking a file in php.
Here is what I am looking for:
I have two files process.php and index.php, both placed in different directories.
This is the full path to process.php file:
/home/happy92a/public_html/ggytg45ffs43456/wp/wp-content/themes/Funizm/loginsystem/process.php
I want to require_once index.php within the process.php file, How can I require it, here is the full path of index.php:
/home/happy92a/public_html/ggytg45ffs43456/wp/wp-content/plugins/plugged/index.php
I have already tried:
dirname(__FILE__) but it gives the path to the current file (process.php) not the (index.php) which I want to include within process.php file.
I have also tried it with $_SERVER['DOCUMENT_ROOT'] but still it does not work also I have read it is bad practice to use server variable.

i am assuming your DOCUMENT_ROOT is at /home/happy92a/public_html/ if so using that as a base to build an absolute path by doing the following.
require_once ($_SERVER['DOCUMENT_ROOT'] . "/ggytg45ffs43456/wp/wp-content/plugins/plugged/index.php");
I read that you do not wish to use DOCUMENT_ROOT. you can then establish a constant for your wordpress installation called WP_DIR
define('WP_DIR', '/home/happy92a/public_html/ggytg45ffs43456/wp/');
require_once (WP_DIR . "wp-content/plugins/plugged/index.php");

Use DOCUMENT_ROOT:
require_once($_SERVER['DOCUMENT_ROOT'] . '/ggytg45ffs43456/wp/wp-content/plugins/plugged/index.php');

try
require_once('./ggytg45ffs43456/wp/wp-content/plugins/plugged/index.php');

Related

' ../ ' not working in php

I have a file frontend/file.php and I want to include a file (using require function) in database/file2.php (frontend and database folders are in the same directory)
I wrote this code:
require('../database/file2.php');
But it isnt working, please help
Use this:
require __DIR__ . '/../database/file2.php';
This will ensure the file is located relative to the current path, read more about this here.
PHP Require Guide

Root path variable in a PHP project

I created a php project like this:
img
js
php
config
config.php
pages
news.php
contact.php
I need to include my config.php into all my files news.php, contact.php
How to avoid to have a relative path like ../config/config?
I would like to have a root path like php/config/config.php.
I use MAMP Pro. Should I use an environment variable to save the root path?
In PHP there is a global variable containing various details related to the server. It's called $_SERVER. It contains also the root:
$_SERVER['DOCUMENT_ROOT']
The only problem is that the entries in this variable are provided by the web server and there is no guarantee that all web servers offer them.
Make a folder in your root. Name it e.g. Helpers/. Make a file in it path.php and inside it put this code.
function base_path($path = "") {
return realpath(__DIR__ . '/../') . DIRECTORY_SEPARATOR . $path;
}
then include this file at the top of your every web document same as you do for your session start and then you can use this function any where in your code. Like so
<?php
require "Helpers/path.php";
require base_path('php/config/config.php');
Create a constant with absolute path to the root by using define in ShowInfo.php:
define('ROOTPATH', DIR);
OR (PHP <= 5.3)
define('ROOTPATH', dirname(FILE));
1 Find out your document root. This value is the same for all your scripts
echo $_SERVER['DOCUMENT_ROOT']; exit;
2 Use the path relative to the document root whenever you include config.php (adjust the line below once you know your document root)
require_once $_SERVER['DOCUMENT_ROOT'].'/php/config/config.php';

require_once and include problems php

Somehow, I cannot include the file I want, neither using require_once, nor include. The file I want to include is in the same directory as the file in which I want to include it. I tried every combination, but still does not work.
The error I get trying to use require_once
Warning: require_once(D:\PROJEKAT\wamp\www\Eif\db_connect.php): failed to open stream: No such file or directory in D:\PROJEKAT\wamp\www\Eif\create_user.php
I first tried with
require_once '/db_connect.php'
then I used
require_once '\db_connect.php' cause I realized that I have to use \ in windows
Then tried
require_once __DIR__ . '\db_connect.php'
And lastly
require_once $_SERVER['DOCUMENT_ROOT'] . '/Eif/db_connect.php';
I think that the path I'm using is good, but it keeps using it on the wrong way.
What is the problem?
Thanks
Quick answer
You should be able to do this:
require_once 'db_connect.php';
Furthermore information
require, require_once, include and include_once are all the same with one exception: require will emit a fatal error on failure where as include will emit warning.
The other thing to bare in mind these actually search through the php ini include_path directive, so when you attempt to include another file php will search through those directories in order of the first directive.
By default this directive usually looks like: .:/usr/share/php:/usr/share/pear. This could be changed by a number of functions, even the php.ini which you would want to be aware of:
set_include_path - Set the include paths directive;
get_include_path - Show the current include paths;
getcwd - Show the current working directory;
chdir - Change the current working directory;
NOTE if using set_include_path you are not adding to the list, but overriding it's value; so it would be wise to know the correct way of setting this is:
set_include_path(get_include_path() . PATH_SEPERATOR . '/path/to/new/location');
If the file you want to include are in the same directory this should work:
require_once 'db_connect.php'
There has to be some mistake in the way you are trying to reach to the path.
You can use following php functions to debug it.
<?php
print_r(get_included_files()); // Write this above the file that you are trying to include
set_include_path('path_to_your_directory');
// This will set the directory for once in<br /> your page after setting the path use require_once with the file name straight away
?>

PHP require path - same directory

I have index.html in the root and all supporting files in /HTML. I have Google analytics code in a file in the HTML directory. It works from my index.html with this code in between the head tags...
<?php
require('HTML/GoogleAnalytics.html');
?>
but not in any of the supporting files in the HTML directory, same directory as the file i'm trying to require/include with this code...
<?php
require('GoogleAnalytics.html');
?>
from PHP.net "...include will finally check in the calling script's own directory and the current working directory before failing"
What am I doing wrong?
From PHP 5.3 (which is at this time after end of life cycle) and later you can use also __DIR__ constant , http://php.net/manual/en/language.constants.predefined.php
require(__DIR__ . '/GoogleAnalytics.html');
To make it relative to the current file, you can prepend dirname(__FILE__) like so:
require(dirname(__FILE__) . '/GoogleAnalytics.html');
By default, paths are relative to the file that the request originated from.
Try using this:
require('/HTML/GoogleAnalytics.html');

How to include file from another directory

In the root (www) I have two folders.
In the first folder, "folder1", I put a file called register.php.
In the next folder, "folder2", I put files called header.php and footer.php.
I need to include the header and footer files from folder2 in the register.php file.
How can i do this? I tried to use this include ../folder2/header.php
..but it does not work
On some configurations, adding ./ (current dir) does the trick like this:
include './../folder2/header.php';
Alternatively, you can specify in terms of document root:
include $_SERVER['DOCUMENT_ROOT'] . 'folder2/header.php';
<?php include( $_SERVER['DOCUMENT_ROOT'] . 'folder2/header.php' ); ?>
include $_SERVER['DOCUMENT_ROOT'] . '/folder2/header.php';
would work from any directory of the site
it is called absolute path and it's the only reliable way to address a file
However, in real it should be something like
include $_SERVER['DOCUMENT_ROOT'] . '/cfg.php';
// some code
include $TPL_HEADER;
using a variable, previously defined in cfg.php
However, it may fail too. Because you can be just wrong about these paths
And here goes your main problem:
but it does not work
There is no such thing as "it does not work"
There is always a comprehensive error message that tells you what exactly doesn't work and what it does instead. You didn't read it yourself, and you didn't post it here to let us show you a correct path out of these error messages.
include files should generally be kept outside of the server root.
lets say your setup is;
www/website1
and
www/includes
Then you php.ini file, or .htaccess file should stipulate that
include_path=www/includes
then from any of your files, in any directory, no matter how far down the trees they go you simply do:
include 'myfile.php';
where myfile.php is at www/includes/myfile.php
Then you can stop worrying about these issues
include dirname(__FILE__).'/../folder2/header.php';
Try This it is work in my case
<?php require_once __DIR__."/../filename.php";?>
As the PHP manual states here $_SERVER['DOCUMENT_ROOT'] is "The document root directory under which the current script is executing, as defined in the server's configuration file." For this example, $_SERVER['DOCUMENT_ROOT'] will work just fine but. . . By using the new "magic constants" provided in >= PHP 5.3, we can make this code a little safer.
Put your includes in a subfolder, and use the magic constant DIR to make a reference to the included files. DIR returns the directory of the currently executing php file. By using this, you can move your folder containing all your includes anywhere you like in your directory structure, and not need to worry if your includes will still work.

Categories