PHP Relative Include with Virtual Directory on IIS7 - php

I have a root configuration virtual directory (named "config" pointing to a directory inside /wwwroot/) specified in IIS7. I want to access this virtual dir programmatically via an "include" function such as:
include '/config/main.php';
I get Warning: require(config/main.php) [function.require]: failed to open stream errors. I have tried the following:
/config/, \config\, ./config/, config/, ~/config/, $SERVER_['DOCUMENT_ROOT']/config/
To no avail. Why does PHP have to be such a dink about site relative references!? Not to rain on the parade, but ASP can handle site relative references (or document relative with ../blah.php) with ease!
Any ideas? I have looked at the set_include_path function and I'm going to try that out in the meantime, but it basically seems like that would be hard-coding my directory paths - annoying for moving code from dev to prod environments.

Try it:
dirname(__FILE__).'\\config\\main.php';

Issue is sort of closed because I couldn't find a satisfactory answer, I simply just rejigged my problem so that there's no virtual directories. Damn PHP and relative paths! It seems to be a problem specifically with IIS.
If anyone finds a better solution the problem I will be more than happy to switch the selected answer.

Related

Set PHP include_path for site folders rather than entire web server

If this should be posted elsewhere please advise. I have reviewed other questions and answers but not been able to resolve my query so far.
I have wampserver running:
Server Software: Apache/2.4.23 (Win64) PHP/5.6.25 - Port defined for Apache: 80
My notebook o/s is Win10.
I am very new to this and just working through a PHP and MySQL book. I am putting multiple example sections in a local website for training so I don't have one area for includes but several with the root of the site structure:
eg: myweb/book01/includes; myweb/book02/includes
echo get_include_path(); shows .;C:\php\pear and I don't want to change the php.ini file as it may work for one myweb/book01 but not myweb/book02
I can set include paths manually for each page using ../ format but as the directory structure changes these fail unless manually edited which doesn't seem the right thing to do.
Is there a way to set an include_path that can be defined and used by specific pages and/or sections rather than a whole web root?
I can then just set once for each section (book01, book02) in the section's header file.
I have tried the following but keep getting 'Warning // failed to open stream' and ' Failed opening ... for inclusion' error messages:
set_include_path(get_include_path().":"."D\data\url\d4th\training\includes");
include $_SERVER['DOCUMENT_ROOT'] . '/training/includes/test_header.inc.php';
set_include_path ('.:/d4th/training/includes');
set_include_path('D:\data\url\d4th\training\includes');
set_include_path('.;D:\data\url\d4th\training\includes');
include ($_SERVER['HTTP_HOST'] . '/training/includes/header.inc.php'); // value for the above is 'd4th'
I'm sure there's a relatively straight forward solution. Been doing this for hours now and can't get my head round it.
You don't need to set the include path.
__DIR__
will always give you a prefix of your current directory.
<?php
include __DIR__.'/includes/test_header.inc.php';
There are much better ways to structure your applications. If you're just learning PHP, here's a great resource:
https://laracasts.com/series/php-for-beginners

Reverting to a different file when PHP fails to find a specific file (include function)

Sorry for you advanced guys, I'm actually teaching myself some PHP so this may seem like a beginner's question.
I'm using a testing server and then uploading to a remote server. The index.php file is located in "C:\XAMPP\htdocs\php_site" on my local pc and in "home/www/myname.atwebpages.com/" on the remote server. Now the code I'm trying to run is just a simple:
define ('ROOT', $_SERVER['DOCUMENT_ROOT']);
include ROOT."menu/menu.php";
This code works fine for the remote server. However, when attempted on my local machine, it spits out this error:
Warning: include(C:/XAMPP/htdocs/menu/menu.php): failed to open stream: No such file or directory in C:\XAMPP\htdocs\php_site\index.php on line 21
Clearly, it's not looking in the php_site folder. Instead, it's tying to find a menu folder in the htdocs directory, but it's not there. The menu folder is located inside the site folder, php_site. If I chance around the code to work on the local machine, it no longer works on the remote server. I'm a little confused as to how to get around this problem.
I think $_SERVER['DOCUMENT_ROOT'] is defined by apache, so you'd need to change the config there. Or, define the ROOT constant relative to where you actually put your files, so if you do something like:
define ('ROOT', dirname(__FILE__));
Put that in a constants file in the same folder as your index.php.
Your document root on the remote and local machines is different. On your local machine your document root is the htdocs directory, and the php_site folder is merely a sub-folder, and thus the path is wrong.
I suggest either making the ROOT directory be a relative directory to the index page, or have a constants file in the root directory of the PHP site that defines the root directory as the directory it is in (which would be in the php_site directory on your local machine, the same directory as your index page). define ('ROOT', dirname(__FILE)); would work in this situation.
Another idea is to use a try-catch to catch the failure of the include statement, and attempt to try another directory, perhaps using define ('ROOT', $_SERVER['DOCUMENT_ROOT']); first, and if it fails, attempt to use define ('ROOT', $_SERVER['DOCUMENT_ROOT'] . 'php_site/'); instead.

php include() from server root?

I'm deploying from my WAMP testing environment to an online test...
Locally I had my include paths something like this:
include('C/wamp/www...')
how do i find the equivalent path on my server?
i've tried using '/' to get to the root but i get this error:
Warning:
require_once(/test123/mvc/views/txt/index_nav_txt.php)
[function.require-once]: failed to
open stream: No such file or directory
in
/home/user/public_html/test123/mvc/views/components/st_footer.php
on line 37
Fatal error: require_once()
[function.require]: Failed opening
required
'/test123/mvc/views/txt/index_nav_txt.php'
(include_path='.:/usr/lib/php:/usr/local/lib/php')
in
/home/user/public_html/test123/mvc/views/components/st_footer.php
on line 37
You would actually need:
require_once("/home/codlife/public_html/test123/mvc/views/txt/index_nav_txt.php");
notice the edition of /home/codlife/public_html/
The initial / Takes you to the root of the server and your code is located inside /home/codlife/public_html/
do you mean
$_SERVER['DOCUMENT_ROOT']
which basically gives you the full path to your working website directory i.e. c:/wamp/www/(windows) or /var/www/vhost/domain.com/httpdocs/ (linux)
You should probably read up on include_path ( http://php.net/include_path ) - This is generally set to include the document root (where your website is) and can be altered so that you don't have to repeatedly include the same paths.
how do i find the equivalent path on my server?
You don't find it - you tell it where it should be. Admittedly this is not always practical when you buy a hosting package (which IME are usually badly supported and come with virtually no documentation).
First thing to note is regardless of where / how the code is hosted, you should always use paths relative to the directories configured on the php include path (or relative to the PHP script initially invoked by the browser request - the '.' entry from the include_path cited in the error) - never absolute paths. You can easily find this out with:
<?php
print ini_get('include_path');
?>
Judging from the path cited in the error message, it appears to be a POSIX system. The root of the filesystem as seen by the webserver might be quite different from the root as seen from your FTP or SSH software, but they are probably the same.
Note that if this is a shared host, then you probably won't have access to put files in /usr/lib/php or /usr/local/lib/php - so your only option is to use a relative path - which is going to get very messy -
You could do some clever coding around this - but do have a look at packages such as Dokuwiki and phpmyadmin to see how they organise the include files in a relocateable way without any dependance on manipulating the php.ini settings.
Alternatively you may be able to override the include_path via .htaccess, e.g.
php_value include_path ".:/home/codlife/public_html:/usr/lib/php:/usr/local/lib/php"
(which would set a base include_path to your document root)
HTH
C.
Use a configuration file where you store things like:
$application_root = '/home/code_life/public_html/';
In this file use all your environment specific variables or constants. When you deploy the application on a different machine, you just update configuration file.
Example:
You have in your root application a folder called settings with settings.php where you can define:
define('DIR_ROOT', dirname(dirname(__FILE__)) . '/');
Now, on every machine, the DIR_ROOT will be the root of your application and you don't have to change anything.

PHP: Include file from different root directory

I have 2 root directories for a site, httpdocs and httpsdocs. I am sure its obvious what the 2 are for. But I want to keep things consistent through-out the site like global navigation. Right now I have two separate files for this (one in each side) and I would like to only have one. This is hard to maintain because each time I change one I have to change the other (if I remember to), it breaks the DRY rule. But if I try to include a file from one side to the other using a relative path it tells me the file path is not allowed because it goes outside the document root. If I try to include it with an absolute URL I get another error saying file access is disabled in the server configuration. (I am on a hosting account so the most I can change as far as server config is limited to .htaccess). So can anyone think of a work-around for this?
Why not put your global include file in yet another directory (lets call it library) and then have each http root have an include file that includes ../library/lib.php, then sets specific paramaters. This gives you the added benifit of your library php files not being in the document root path as well.
And actually. Updating because I just read the entry about "relative path" issues.
Could you set the "include path" php value to include that directory?
Something like this:
ini_set('include_path', realpath(dirname(__FILE__)."/../library").":".ini_get('include_path'));
require_once('lib.php');
Did a little more research - seems that changing open_basedir is not possible unless you are able to edit the httpd.conf or php.ini values. PHP Manual: open_basedir
Do you have the ability to create symbolic links between the two directories?

Warning on Include: Can't Find File In Correct Path?

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.

Categories