PHP require path - same directory - php

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');

Related

linking to php includes

I'm using a PHP include for my header/navigation, but some pages are unable to find that file. I'm using a root-level link
<?php include("/includes/masthead.php"); ?>
but pages outside of the root folder are not locating the masthead file. For instance:
index.php locates and processes masthead.php just fine;
adopt/adoptadog/php returns an error saying the file does not exist.
Is this because PHP doesn't process links in the same way that HTML does, so my root-relative link just isn't being interpreted by the php include function?
I'd like to be able to have a root-relative link work in my include statement so that statement can go into an Expression Web template. The template seems to write the same address in every page regardless of the location of the page. Maybe it doesn't see a link within a PHP tag the same way it does in HTML--I don't know.
I hope this is clear. Any help?
You could use the document root as the anchor:
include $_SERVER['DOCUMENT_ROOT'] . '/includes/masthead.php';
If your included files are one level outside of the document root, you just need to move with it:
include dirname($_SERVER['DOCUMENT_ROOT']) . '/includes/masthead.php';
If you have a script that gets loaded by all your pages that resides on the document root itself, you can use a constant:
define('PROJECTDIR', dirname(__FILE__));
To include the mast head:
include PROJECTDIR . '/includes/masthead.php';
Try putting this at the top of your script:
set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT']);
Or go to your php.ini file and update the include_path directive to always search your webroot directory.

File linkage in 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');

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.

Relative include files

I have a file
workers/activity/bulk_action.php which includes a file
include('../../classes/aclass.php');
Inside aclass.php it does:
include ('../tcpdf/config/lang/eng.php');
It seems that the include in the second file is using the first files working directory instead of being relative to itself, resulting in an error. How does this work?
You can adapt the second include with:
include (__DIR__.'/../tcpdf/config/lang/eng.php');
The magic constant __DIR__ refers to the current .php file, and by appending the relative path after that, will lead to the correct location.
But it only works since PHP5.3 and you would have to use the dirname(__FILE__) construct instead if you need compatibility to older setups.
You would be way better off by setting a proper value to the include_path and then use paths relative to this directory.
set_include_path(
get_include_path() .
PATH_SEPARATOR .
realpath(__DIR__ . '/your/lib')
);
include 'tcpdf/config/lang/eng.php';
include 'classes/aclass.php';
I also suggest you take a look at autoloading. This will make file includes obsolete.
Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include() will finally check in the calling script's own directory and the current working directory before failing.
You can use dirname(__FILE__) to get a path to the directory where the currently executed script resides:
include(dirname(dirname(__FILE__)) . '/tcpdf/config/lang/eng.php');
(since PHP 5.3, you can use __DIR__)
Or, define a constant in the first file that points to the root directory and use it in your includes.

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