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.
Related
this is kind of a silly question, but as I can't sort it out I thought it might be good to get some help. The point is that the ".. /" to go back directory is not working.
The file I'm executing is in a folder that's on the main route and I need to go back to the main route and then enter another folder to load this other PHP file but it's not working what could be causing this issue.
ERRORS:
Warning: require_once(../PHPMailer/PHPMailerAutoload.php): failed to open stream: No such file or directory in things/public_html/classes/Mail.php on line 3
Fatal error: require_once(): Failed opening required '../PHPMailer/PHPMailerAutoload.php' (include_path='.:/opt/alt/php71/usr/share/pear') in things/public_html/classes/Mail.php on line 3
DIRECTORY STRUCTURE:
File where the requiere once is:
/public_html/classes/filethatwantstoacces.php
File where it wants to get:
/public_html/PHPMailer/PHPMailerAutoload.php
require_once('../PHPMailer/PHPMailerAutoload.php');
What you should be using is the $_SERVER['DOCUMENT_ROOT'] variable. Please read this answer to another question for details.
If you are using PHP you should get into a habit of NOT using relative file paths at all but to use absolute paths, which will guarentee to succeed every time (As long as the target file exists and is reachable, etc.).
so; use $_SERVER['DOCUMENT_ROOT']
As a side note, you do not need to use brackets for your includes/requires, it's simply giving the server more work to do for no extra benefit.
The $_SERVER['DOCUMENT_ROOT'] is the base directory of your PHP/web application, typically the contents of the folder /public_html.
Using correct syntax and the above $_SERVER value (which will point to the /public_html folder you will have:
require_once $_SERVER['DOCUMENT_ROOT'].'/PHPMailer/PHPMailerAutoload.php';
This will work from any script within your directory structure, if the file (PHPMailerAutoload.php) exists and is reachable at that given location
Given your location
/public_html/classes/filethatwantstoacces.php
doing ../ gives you
/public_html/classes
so ../PHPMailer/PHPMailerAutoload.php evaluates to
/public_html/classes/PHPMailer/PHPMailerAutoload.php
As #Martin has pointed out, using $_SERVER['DOCUMENT_ROOT'] to construct an absolute path to your file is the easiest way to avoid relative directory navigation errors such as this:
require_once $_SERVER['DOCUMENT_ROOT'].'/PHPMailer/PHPMailerAutoload.php';
I am an amateur web developer and I am trying to get my site live for the first time. The site is working and all of the files are uploaded but the site is not loading my PHP includes.
Here is the error message:
Warning: include(includes/header.php) [function.include]: failed to open stream: No such file or directory in /home4/myUsername/public_html/index.php on line 3
How can I get PHP to look in public_html/ rather than public_html/index.php?
EDIT: I have tried editing the include path. It doesn't seem to change where php is looking for the file. Additionally my includes work properly in localhost.
I'm going to assume this is your folder structure:
public_html/index.php
public_html/includes/header.php
Generally (not always), $_SERVER['DOCUMENT_ROOT'] will now reflect the path to the base public_html directory (this I'm assuming based on the context of your message). This means you can always point to the root this way. - no matter if you have /index.php or /my/deep/file/structure.php
Try this with your include statement on index.php
<?php
include($_SERVER['DOCUMENT_ROOT'] . '/includes/header.php');
You may need to change the include path in your php.ini file or use set_include_path() to change the include path.
Here is the manual entry for the function call if you'd like to read more about it.
Have you checked already the include file?
in given. include(folder_includes/file_header.php);
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 _.
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.