I'm a total PHP noob and am using a pretty simple PHP include:
<?php include("~head.php"); ?>
to do a bit of templating for a website (to achieve common headers, footers, menus for all my pages).
NOTE: The tilde (~) is simply there to make the directories easier to look at (pushes main files to the top of the list when sorted alphabetically)
It's working great for files that are in the same directory but when I reference a file outside of a directory, like so:
<?php include("../~head.php"); ?>
However, it simply doesn't seem to be finding the file as the header is clearly not being pulled into the markup.
Conversely, if I reference the file with a full url, e.g.
<?php include("http://example.com/~head.php"); ?>
I get the following error code on my page.
Warning: include() [function.include]: URL file-access is disabled in the server configuration in /home/content/65/7392565/html/bikini/angela_bikini.php on line 1
Warning: include(http://example.com/~head.php) [function.include]: failed to open stream: no suitable wrapper could be found in /home/content/65/7392565/html/products/product_a.php on line 1
Warning: include() [function.include]: Failed opening 'http://example.com/~head.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /home/content/65/7392565/html/products/product_a.php on line 1
Strangely, the "../file.php" syntax works for non-header files (e.g. the include I'm using for the menu).
As such code's gotten to be a bit of a fragmented mess and is difficult to maintain changes across all the different pages. Any thoughts or solutions would be very much appreciated. I really am a noob tho so I probably won't be able to wrap my head around anything too fancy. : )
Thanks for your time.
Jon
Rather than using only the ../ to get the directory above, a construct like this will create the full filepath:
// Assuming you are including from the root
$application_path = dirname(__FILE__);
include("$application_path/../header.php);
Typically I'll do this by defining a constant, rather than using a variable.
define('APP_PATH', dirname(__FILE__));
Use this as:
// Assuming you are including at the file root:
define('APP_PATH', dirname(__FILE__));
include(APP_PATH . "/include/head.php");
// Assuming you are including from /include (one directory in)
// append a "/../" onto the end to indicate that the application
// root is one directory up from the currently executing file.
define('APP_PATH', dirname(__FILE__) . "/../");
include(APP_PATH . "somefile_at_the_root.php");
You have to be careful with the tilde! Under UNIX-like operating systems, the tilde is a shortcut to your home directory. If maybe the Apache server runs under the account www, your file-reference could be interpreted like this:
/home/www/head.php
And for the approach of using the full URL, the error says all:
URL file-access is disabled in the server configuration
Ignoring that it isn't best practice to use full URLs (because your folder structure could change etc.), you have to enable allow_url_include in your php.ini (see PHP.net).
If you really want to have your important files on top, you could use the underscore _.
Related
I have been inspecting code, reading questions on StackOverflow, but I simply don't get it, or rather I dont understand the explanations and / or logic behind it.
Consider The Following
If I have a directory structure like this
Now I want to set the head.php file to be globally accessible throughout the application (just as an example)
define('Head', __DIR__ .'/views/head.php');
If I do the above, I get the following result:
C:\xampp\htdocs\carRental/views/head.php"
Which is technically what I want,, however, notice the URL contains forward and backslashes?
Can I get access to the head.php file by calling Head anywhere in my directory tree?
Im sorry, Ive been inspecting code and read the manual and questions on here, if anyone could give a rookie a clear explanation it would be greatly appreciated.
UPDATE:
When I try to do the following in landingPage.php I get the following errors
include_once Head;
Notice: Use of undefined constant Head - assumed 'Head' in
C:\xampp\htdocs\carRental\views\landingPage.php on line 2
Warning: include_once(Head): failed to open stream: No such file or
directory in C:\xampp\htdocs\carRental\views\landingPage.php on line 2
Warning: include_once(): Failed opening 'Head' for inclusion
(include_path='C:\xampp\php\PEAR') in
C:\xampp\htdocs\carRental\views\landingPage.php on line 2
When you used define('Head', __DIR__ .'/views/head.php'); you have hardcoded the slashes in the definition.
However windows by default uses \ as the default directory separator so __DIR__ will be using \ in the path when in Windows (it's ok with using / as an alternative one though so it shouldn't be a problem).
You can do the following if you want them to be consistent:
define('Head', __DIR__ .DIRECTORY_SEPARATOR ."views".DIRECTORY_SEPARATOR ."head.php");
Which is technically what I want,, however, notice the URL contains forward and backslashes?
yes
Can I get access to the head.php file by calling Head anywhere in my directory tree?
no
The mix of foreward and backward slashes is created by your command
__DIR__ creates C:\xampp\htdocs\carRental and '/views/head.php' is the string you append.
To be able to use your defined HEAD you would have to load the php file which defines it. another php files does not know what this file does or doesn't do as long as it is not persisted. (which you don't do in the code provided)
To load a file and make your definition available use include_once / require_once
__DIR__ will always resolve to the absolute directory of the file using it.
The reason for forward and backslashes. This part:
C:\xampp\htdocs\
Is Windows file path.
This part:
carRental/views/head.php
Is the webserver path, i.e not Windows.
Your define will hold the correct file path so try to include it now:
include_once Head;
I'm developing a site on my local wamp stack. I have created an alias to view the site so i go to localhost/eee/ to view it. Ideally i would like to go to www.eee.lo but ever since upgrading to win8 I can't get it to work.
So this is the problem, i'm making modules for the website so i don't have to change all the code etc... And i don't want to have to go around changing all the url's when i migrate to the online server so i'm creating a file called _control.php which has this;
$_SITELOC = "localhost/eee/";
And then each time i want to include a file i will go;
include "$_SITELOC/scripts/inc/_header.php";
But this doesn't work and i can't work out why as if i echo it rather than include it and then i take what it prints and put it into the url it goes to the correct file. But it throws errors on the include, it gives two warnins;
Warning: include(localhost/eee/scripts/inc/_header.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in C:\Users\Chris\Documents\EEE\Website\Site\index.php on line 3
Warning: include() [<a href='function.include'>function.include</a>]: Failed opening 'localhost/eee/scripts/inc/_header.php' for inclusion (include_path='.;C:\php\pear') in C:\Users\Chris\Documents\EEE\Website\Site\index.php on line 3
I read somewhere that it might be to do with the include path so i tried;
set_include_path(get_include_path() . PATH_SEPARATOR . $_SITELOC."/scripts/inc/");
but this too did not work and now i'm not sure where to go.
Thanks, Chris
localhost/eee/ is your public address that you can use in your web browser. This public address should more appropriately be written as http://localhost/eee/. When you move to web server, you get the public address http://www.eee.lo/.
When including files, you have to use file paths. For example, if you have your www (or httpd, whatever) directry in D:\ on windows, then your include path should start with D:\www\eee\.
So, basically you have to use two variables to keep paths.
$_SITELOC = "http://localhost/eee/"; //For all URLs used in your HTML document.
$_INCPATH = "D:\www\eee\\"; //For all internal file includes.
In practice, you will need both of these, and it is good practice to keep the website address and internal paths out of your main script because when uploaded to remote server, not only your public address changes, but you will also have to deal with absolutely different internal (include) paths.
Your idea is basically good, to define one (root) path of the application and include files based on it, but unfortunately you're not doing it quite right. You have basically two ways of doing that.
One way (which I personally find better) is to include local files in your file system, where you can define the root path, i.e. like
define ('ROOT', 'your/document/root/path');
// and then include the files
include ROOT . '/' . '/scripts/inc/_header.php';
The other way would be to include a web resource, what you're trying to do, but you've forgotten to specify the scheme (protocol) you want to use, i.e.
define ('ROOT', 'http://localhost/eee');
// and then include the files
include ROOT . '/' . '/scripts/inc/_header.php';
For more information, see the examples, provided by the documentation for include
Note: If you want to include the source of a php file, i.e. file with definitions of functions, etc., use the first approach. Including files, using the second approach will only include the output produced by that file.
If you include() a URL, you will (probably) be including the output of the script's execution, when you want to include the script's source. It seems like you actually want to include by local file system path.
i'm getting following error in my PHP file.
Warning: include(../config/config.php) [function.include]: failed to open stream: No
such file or directory in C:\xampp\htdocs\my-proj\functions\function.php on line 2
let me describe my folder structure
ROOT folder /index.php
functions / function.php
config / config.php
signup / signup.php
now, If i use absolute path, then it is give the same error in signup.php, and if I use relative path then it is giving this error in index.php
any help would be appreciated.
use
include("$_SERVER[DOCUMENT_ROOT]/config/config.php");
The file paths are relative to the invoked script. If your application gets invoked by http requests to index.php, then the include() path needs to be relative to that - even if the include statement itself is located in the functions.php script.
A common workaround is to make all paths absolute in relation to the document root:
include("$_SERVER[DOCUMENT_ROOT]/config/config.php");
// Note: leaving out array keys only valid in double quote string context.
That would work in index.php and functions.php alike.
Use include __DIR__."/../config/config.php"; if you want to include a file relative to the file you're currently executing in. If you're using a version of php older than 5.3.0 (you shouldn't), replace __DIR__ with dirname(__FILE__).
$_SERVER['DOCUMENT_ROOT'] is not set when using commandline and requires that your project is relative to the DOCUMENT_ROOT instead of allowing the user to place it wherever they please. If phpMyAdmin used this variable, you would be forced to accommodate it instead of just placing it wherever you want. That's another thing, it's a variable, so there's a potential security issue too.
If config.php is necessary, I suggest using require instead, otherwise use if (file_exists($file)) {require $file;} so you can avoid warnings when it doesn't exist and get an error when it can't be read (I assume if it exists, it's intended to be used).
I get the following error in one of my function includes.
require(../htmlpurifier/library/HTMLPurifier.auto.php) [function.require]: failed to open stream: No such file or directory
I can correct this problem if I code the path
./htmlpurifier/library/HTMLPurifier.auto.php
But I want to display the include file in many different levels of folders on my website without having to re-code the path inside my function include every time which defeats the purpose of my include. Is there a way I can have this work with out having to re-code the path every time? Is there a way to include the full path?
Try this:
require realpath(dirname(__FILE__) . '/htmlpurifier/library/HTMLPurifier.auto.php');
That way you always include files relative to the path of the current file.
You are using wrong path.
Just put these 2 lines together and feel the difference.
../htmlpurifier/library/HTMLPurifier.auto.php
./htmlpurifier/library/HTMLPurifier.auto.php
That's 2 different paths and will never work at the same time. Latter one points to the current directory while first one - one level higher.
But I want to display the include file in many different levels of folders on my website without having to re-code the path inside my function
that's sensible desire. Here you go:
require $_SERVER['DOCUMENT_ROOT'].'/htmlpurifier/library/HTMLPurifier.auto.php';
(assuming this htmlpurifier folder lays in the document root)
Try this
require (dirname(__FILE__).'/../htmlpurifier/library/HTMLPurifier.auto.php');
How about configuring the path to this .php file as a configuration variable in your script? Presumably you'll be loading some common functions library in all your pages, so just do:
$PATH_TO_PURIFIER = 'C:\this\that\whatever\HTMLPurifier.auto.php';
in that common file, then you can just do
include('commonstuff.php');
include($PATH_TO_PURIFIER);
As long as commonstuff.php is somewhere in your PHP's include_path, it's far more reliable to do it this way rather than try to dynamically calculate the path everywhere else. Especially if your directory structure might change, rendering the calculated path invalid and forcing you to change every PHP file yet again.
Warning: include(/2008/2009/assets/inc/base/error.inc.php) [function.include]:
failed to open stream: No such file or directory in
C:\Program Files\Apache Group\Apache2\htdocs\2008\2009\assets\inc\base\header.inc.php on line 82
I am receiving the above error when including a file on a WAMP setup. Is it possible that Windows is interpreting the /2008/ to mean c:/2008 rather than the actual http://localhost/2008?
To give a bit more detail, I have a constant defined as ROOT that I use all over my site. (ROOT in this instance is set to /2008/2009/). I use ROOT before paths for images, links, css files, include/require files, etc. The CSS and links are properly scaling to localhost/2008/2009, but include files are not being found.
Any help would be greatly appreciated.
PS php.ini has include path defined as:
include_path = C:\Program Files\Apache Group\Apache2\htdocs"
Update
I was using URL rewriting to change item/x to item-display.php?id=x, this change in folders is what ROOT was built off of. This made anything displayed to the browser (imgs, links, etc) all work flawlessly, but made anything internal not work, as item-display.php is actually one folder down. I moved item-display.php to an item folder, and this made the actions consistent both client side and server side.
You’re using an absolute path rather than a relative path.
If you are using the include path, you have to specify a relative URL, starting with the first directory or ./. Btw, you should define your path with something like dirname(__FILE__) in a file whose position in your project is not going to change or by looking into server. Otherwise, installing your application on another server is going to be unnecessarily complicated.