There has to be something I'm overlooking but I can't seem to get my includes to work as expected using relative paths. In MAMP the DocumentRoot is configured in httpd.conf like this:
# MAMP DOCUMENT_ROOT !! Don't remove this line !!
DocumentRoot "/Applications/MAMP/projects/journalproject”
I bring up the site at http://localhost:8888/ and the includes work fine if I use a path like this:
<?php include('nav.php'); ?>
But, if I put my include file into a folder, the include doesn't show up on the page:
<?php include('/includes/nav.php'); ?>
This is the path of the include file: /journalproject/includes/nav.php
I'm calling it from here: /journalproject/journals/index.php
I can also get the include to work if I use a path like this:
<?php include('../includes/nav.php'); ?>
I'm not sure why I need to specify the path for the include when an anchor link has no trouble finding the same file using a relative path:
Find nav include
I'm not sure where to go from here. Any help would be greatly appreciated.
I believe that the below doesn't work because it is being treated as an absolute path, as it starts with a "/" which tells PHP to look from the literal "/" directory in Unix Operating Systems:
<?php include('/includes/nav.php'); ?>
Try:
<?php include('includes/nav.php'); ?>
you can specify files relative to the include path without being conscious of where your specific .php file that calls the include/require is located:
http://php.net/manual/en/function.set-include-path.php
set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'])
Related
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.
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.
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');
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.
I like to work on websites locally before uploading to my host. I use PHP/MYSQL servers in an XAMPP install.
I have multiple directories in XAMPP htdocs directory (one for each project). Each project usually has at least:
header.php
index.php
footer.php
styles/stylesheet.css
This worked fine until recently.
I am now working on a more extensive file/directory structure. Now, when /about/index.php calls header.php, the path to the stylesheet directory doesn't point in the right direction. Image paths no longer point in the right place either since they are all relative paths.
I tried pointing everything to the home directory first using a "/" at the beginning of every path, but in XAMPP the home directory now refers to localhost, instead of the directory for the particular project.
What is the solution? Is there a better way to be working on projects locally so I can upload to my web host simply, using all relative paths and not having to change them for live and dev versions of the website?
The simplest solution as answered by #Vladimir Dimitrov in this thread goes as following (I will just copy it here):
The easiest way is to create separate virtual host for each site folder in /htdocs So you will access the http:// mysite.local instead of http:// localhost/mysite
There are two things to do: 1. edit C:\xampp\apache\conf\extra\httpd-vhosts.conf (by default) adding something like:
<VirtualHost *:80>
ServerName mysite.local
DocumentRoot C:/XAMPP/htdocs/mysite
</VirtualHost>
edit c:\windows\system32\drivers\etc\hosts adding
127.0.0.1 mysite.local
restart xampp and try http://mysite.local
Possibly this helps you?
You have to edit some config to reference each site to a base-link.
creating-multiple-sites-on-a-local-web-server
You could try this:
create a common configuration file
define a BASE_URL constant to your home directory (e.g. http://localhost/my_project/)
in your templates use all your links and references with BASE_URL
When you will deploy, you will need to change only one file.
You could also set a BASE_PATH constant to your directory (e.g. c:/xampp/htdocs/my_project). This might be useful when trying to include scripts from various sub-directories, without "guessing" the local path. (e.g. include BASE_PATH . 'templates/my_template.php')
Try including them using `DOCUMENT_ROOT for your PHP files, ie:
include($_SERVER['DOCUMENT_ROOT']."folder/header.php");
This assumes, when looking in the browser, header.php can be ound by going http://127.0.0.1/folder/header.php
For other files, such as CSS, Javascript you could define the location as follows:
define("SCRIPTS_URL", "http://127.0.0.1/_scripts/");
Include the above in your header.php file, and make sure you include header.php before calling the actual html header, eg:
<?php
include($_SERVER['DOCUMENT_ROOT']."folder/header.php");
?>
<html>
<link rel="stylesheet" type="text/css" href="<?php echo SCRIPTS_URL; ?>stylesheet.css">
... etc etc ...
You can further combine define and build up directory parts, for example:
$project = "project_x";
include($_SERVER['DOCUMENT_ROOT'].$project."/header.php");
define("SCRIPTS_URL", "http://127.0.0.1/".$project."/_scripts/");
If you do it like above, then you only need to change the project variable, if you see...
Update
The below would be index.php:
<?php
// Make the header relative to index.php (as we don't know the project) - assume header is located at /_template/header.php and this file is located at /index.php [if, in future you have /content/index.php - then the below would be ../_template/header.php, etc]
if(file_exists("_template/header.php")){
include_once("_template/header.php");
} else {
die('Fatal error - no header found');
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="<?php echo BASE_URL; ?>styles/stylesheet.css">
</head>
<body>
// Content goes here
</body>
</html>
<?php
if(file_exists(ROOTPATH."_template/footer.php")){
include_once(ROOTPATH."_template/footer.php");
}
?>
And header.php:
<?php
define("PROJECT_NAME", "project_x");
define("ROOTPATH", $_SERVER['DOCUMENT_ROOT'].PROJECT_NAME."/");
define("BASE_URL", "http://".$_SERVER['SERVER_NAME']."/".PROJECT_NAME."/"); // $_SERVER['SERVER_NAME'] automatically puts 'localhost' or the domain name in automatically
?>
As you can see - everything is defined in this header file and when it is included on index.php - index.php can access those definitions, as can any other file that is included after the definition has been made (note that you cannot overwrite a definition and cannot define the same definition twice.
I solve this problem by defining some constants:
# index.php
# handles pretty much everything for the site
define( 'DS', DIRECTORY_SEPARATOR );
define( 'ROOT', dirname(__file__) );
define( 'HOST', 'http://' . $_SERVER['HTTP_HOST'] . dirname( $_SERVER['PHP_SELF'] ) );
Then, for includes, I do something like this:
include ROOT . DS . 'directory' . DS . 'file_i_want.php';
For CSS and whatnot, it may be easier to just set the base URL in the markup.