For my website www.mysite.com I have a folder structure like this:
root
/login
/includes
In the folder login I have login.php with (simplified):
<?php
include_once('../includes/includes.php');
?>
If I access www.mysite.com/login/login.php the include works just fine, but if I access login.mysite.com/login.php the include doesn't work.
How can I get this to work?
I've tried using $_SERVER["document_root"] in the include, and I've tried with www.mysite.com/includes/includes.php.
Suggestions are much appreciated.
Edit: Unfortunately I don't have access to php.ini as I am on a web-hotel.
easy way with ssh you can Symlink file to anther locate file
ln -s my_folder/includes.php includes2.php
then use your include
include_once('includes2.php');
Thanks to #Professor Abronsius:
a combination of __DIR__ (magic constant), chdir(), getcwd() and set_include_path() are most useful when setting a path to files for inclusion (either using require or include etc ). You can store a reference to the current working directory with getcwd() for later use if you set different include paths or whatever.
Related
here is my project structure:
Web root/document root:
/var/www/ all the website files in the the 'www' folder
I want to use require or include a file inside my folder OUTSIDE the web root/document root:
/var/extra-files
how can i get a file inside the extra-files folder using require or include?
require "OUTSIDE DOCUMENT ROOT/extra-files
thanks
First make sure apache as permission to read the files on /var/extra-files, then you can use:
require "/var/extra-files/file.php";
You may want to read Difference between require, include and require_once?
Hmm wow... no way was it that easy. I just used this:
/var/extra-files/myfile.php
and it worked.
I have a directory root:
index.php
includes/
template.php
testfile.php
phpFiles/
processInput.php
testfile.php
index.php:
require_once("includes/template.php");
template.php:
require_once("includes/phpFiles/processInput.php")
processInput.php:
require_once("testfile.php")
require_once("../testfile.php")
This code will work when you run index.php, of course it will not work when you run template.php.
As you can see, index.php includes template.php like normal. But in template.php, you have to include like if you are in the directory that index.php is in. But then, in processInput.php, you include as if you are in the directory that processInput.php is in.
Why is this happening, and how can I fix it so that the include path is always the directory of the file that the require is done in? The second included file have the same include path as the requested file, but the next one does not.
Thanks for your help!
EDIT: The strange thing is that I've included classes in a class folder. And it included other files as it is supposed to, even though the paths are relative. WHY does this happen, and how can I fix it?
VERY IMPORTANT EDIT: I just realized that all this is because in my example, the inclusion in includes/phpFiles/processInput.php includes a file in the same directory: require_once("file in same dir.php"); This is the reason. If you are including a file with out specifying anything more than the filename, the include_path is actually the dir where the file the require is written in is in. Can anyone confirm this?
Use an absolute path.
require_once($_SERVER['DOCUMENT_ROOT']."/includes/phpFiles/processInput.php");
Use a similar form for all your required files and they will work no matter where you are.
You can do this in a few ways, amongst others:
Use set_include_path to control the directories from where to perform require() calls.
Define a common absolute base path in a constant that you define in index.php and use that in every require() statement (e.g. require(BASEPATH . '/includes/template.php')).
Use relative paths everywhere and leverage dirname(__FILE__) or __DIR__ to turn them into absolute paths. For instance: require(__DIR__ . '/phpFiles/processInput.php');
By default, the current working directory is used in the include path; you can verify this by inspecting the output of get_include_path(). However, this is not relative to where the include() is made from; it's relative to the main executing script.
You're using relative paths. You need to use absolute paths: $_SERVER['DOCUMENT_ROOT'].
When you include/require, you are basically temporarily moving all code from one file, to another.
so if file1.php (which is located in root) contains:
require("folder/file.php");
and you include file1.php in file2.php (which is in a different location (say folder directory for example):
file2.php:
require("../file1.php");
Now all of file1.php code is in file2.php. So file2.php will look like this:
require("../file1.php");
require("folder/file.php");//but because file2.php is already in the `folder` directory, this path does not exist...
index.php:
require_once("includes/template.php");
template.php:
require_once("includes/phpFiles/processInput.php")
Your directory structure is off. The file inclusion is being seen from the file you're using it from. So, "template.php" is looking for an "includes/" folder in its current folder (/includes/).
As others are saying, use absolute paths, which will make sure you're always going at it from the file system root, or use:
require_once("phpFiles/processInput.php")
In your template.php file (which is far more likely to break if you ever move things around, which is why others all recommend using absolute paths from the file system root).
BTW, if you're using "index.php" as some kind of framework system, you can consider defining a variable that stores the address of common files such as:
define('APPLICATION_PATH', realpath(dirname(__FILE__));
define('PHPFILES_PATH', APPLICAITON_PATH . '/includes/phpFiles/');
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.
following is my files directory structure...
Config
config.php (is calling some files too like includes/class.DB.php)
Now i have created another folder Admin and created new config.php (and calling root config file by require_once '../config/config.php';) its loading config file correctly but showing errors on includes/class.DB.php .
I hope, you have got my an idea of my problem, what is the way to achieve this, by USING DIR_NAME/ $SERVER['DOCUMENT_ROOT']
please help.
use
dirname(__FILE__); // or, if you have +5.3, use __DIR__ instead
so if you have this structure
includes
-class.php
admin
-admin.php
config
-config.php
So you can use
//admin.php
include(dirname(__FILE__)."/../config/config.php");
//config.php
include(dirname(__FILE__)."/../includes/class.php");
it always aims to the same directory!
Try:
include(dirname(__ FILE__)."/../include/connection.php");
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.