473,387 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

includes are really messing me up

Hi,
I have these files

/include/db_info.inc
/html_root/util_fns.inc
/html_root/home_page.php
/html_root/admin/admin_home.php

The page "util_fns.inc" contains the line

<?php include("../db_info.inc"); ?>

The page home_page.php contains

<?php include("util_fns.php"); ?>

and admin_home.php contains

<?php include("../util_fns.php"); ?>

but when I visit admin_home, I get the error
"main(../include/db_info.inc): failed to open stream: No such file or
directory". If I change the line in "util_fns.inc" to:

<?php include("../../db_info.inc"); ?>

the admin_home.php works fine, but the home_page.php page breaks. How
can I write the include line in "util_fns.inc" to accommodate both
situations?

Thanks, - Dave
Jul 17 '05 #1
3 1674
D. Alvarado wrote:
Hi,
I have these files

/include/db_info.inc
/html_root/util_fns.inc
/html_root/home_page.php
/html_root/admin/admin_home.php

The page "util_fns.inc" contains the line

<?php include("../db_info.inc"); ?>

The page home_page.php contains

<?php include("util_fns.php"); ?>

and admin_home.php contains

<?php include("../util_fns.php"); ?>

but when I visit admin_home, I get the error
"main(../include/db_info.inc): failed to open stream: No such file or
directory". If I change the line in "util_fns.inc" to:

<?php include("../../db_info.inc"); ?>

the admin_home.php works fine, but the home_page.php page breaks. How
can I write the include line in "util_fns.inc" to accommodate both
situations?

Thanks, - Dave

What is messing things up is that the directory the file is in is where
the include is oriented from.
To make the orientation the same use $_SERVER['DOCUMENT_ROOT']
as the base part in all the files and the includes too.

Therefore
include($_SERVER['DOCUMENT_ROOT'] . "../db_info.php");
if the include_path has /include in it.

or

include($_SERVER['DOCUMENT_ROOT'] . "../include/db_info.php");
if the include_path does not..



Jul 17 '05 #2


/include/db_info.inc
/html_root/util_fns.inc
/html_root/home_page.php
/html_root/admin/admin_home.php

The page "util_fns.inc" contains the line

<?php include("../db_info.inc"); ?>

The page home_page.php contains

<?php include("util_fns.php"); ?>

and admin_home.php contains

<?php include("../util_fns.php"); ?>

but when I visit admin_home, I get the error
"main(../include/db_info.inc): failed to open stream: No such file or
directory". If I change the line in "util_fns.inc" to:

<?php include("../../db_info.inc"); ?>

the admin_home.php works fine, but the home_page.php page breaks. How
can I write the include line in "util_fns.inc" to accommodate both
situations?


Make your include_path setting

include_path /path/to/include/

where /path/to/ is the path to the first of the items you listed.

Then just use

<?php include ('db_info.inc'); ?>
or better still, use require_once () instead of include ()
Martin
Jul 17 '05 #3
D. Alvarado <la***********@zipmail.com> wrote or quoted:
I have these files

/include/db_info.inc
/html_root/util_fns.inc
/html_root/home_page.php
/html_root/admin/admin_home.php

The page "util_fns.inc" contains the line

<?php include("../db_info.inc"); ?>

The page home_page.php contains

<?php include("util_fns.php"); ?>

and admin_home.php contains

<?php include("../util_fns.php"); ?>

but when I visit admin_home, I get the error
"main(../include/db_info.inc): failed to open stream: No such file or
directory". If I change the line in "util_fns.inc" to:

<?php include("../../db_info.inc"); ?>

the admin_home.php works fine, but the home_page.php page breaks. How
can I write the include line in "util_fns.inc" to accommodate both
situations?


The best solution I have found so far is the following code:

function include_once_relative($path) {
$from = realpath(dirname($_SERVER["SCRIPT_FILENAME"]));
$to = realpath(dirname(__FILE__)."/".$path);

include_once(relative_path($to,$from));
}

function relative_path ($targetfile, $basedir = '.') {
$basedir = realpath ($basedir);
$targetfile = realpath ($targetfile);

// on windows, check that both paths are on the same drive
if (substr ($basedir, 0, 1) != substr ($targetfile, 0, 1)) {
return false;
}

// split each path into its directories
$base_parts = split ('\/', str_replace ('\\', '/', $basedir));
$target_parts = split ('\/', str_replace ('\\', '/', $targetfile));

// ensure that there are no empty elements at the end (c:\ would cause it)
for ($i = count($base_parts) - 1; $i >= 0; $i--) {
if ($base_parts[$i] == '') {
unset ($base_parts[$i]);
} else {
break;
}
}
for ($i = count($target_parts) - 1; $i >= 0; $i--) {
if ($target_parts[$i] == '') {
unset ($target_parts[$i]);
} else {
break;
}
}

// get rid of the common directories at the beginning of the paths
$common_count = 0;
for ($i = 0; $i < count($base_parts); $i++) {
if ($target_parts[$i] == $base_parts[$i]) {
$common_count++;
} else {
break;
}
}
for ($i = 0; $i < $common_count; $i++) {
unset ($base_parts[$i]);
unset ($target_parts[$i]);
}

// build the resulting string
$cnt = count($base_parts) - 1;
if ($cnt < 1) {
$cnt = 0;
}

return str_repeat ('../', $cnt).implode('/', $target_parts);
}

Stick it into a library file which you /can/ include - and then
you can use:

"include_once_relative" in place of "include_once" - and it takes
account of a range of situations - including dealing with the
problem of the case where the path being specified is relative
to another included file - rather than the page being displayed.

Any improvements welcomed.
--
__________
|im |yler http://timtyler.org/ ti*@tt1lock.org Remove lock to reply.
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Yannick Turgeon | last post by:
Oups! I did a typing error in my last post. Fixed. ----------- Hello all, We are currently changing our web server and, in the process, updating PHP version from 4.3.0 to 4.3.5. The problem...
7
by: MyndPhlyp | last post by:
I am building up a library of Class'es that represent various columns of table layouts. One often used column is "name" and each occurrence is treated identically. (What a concept, eh?) Part of...
8
by: Jim | last post by:
Hi: Do we have some common style for includes when working on a project with lots of c and h files. Wat I mean is do we have a rule in C when a file includes several files and those file in turn...
10
by: Ben Taylor | last post by:
Hi, Coming from VB, I've still not really grasped the way how in C++ if function A wants to call function B, then function B has to be before function A in the compilation, and #includes that use...
12
by: tshad | last post by:
I am not sure why I am getting this error: I have the following code I want to run from another include file that holds all my functions. functions.inc...
53
by: Steven Woody | last post by:
hi, after some weeks of development of a project, there likely are many "#include" in source files which was added before but are now unnecessary. is there any tool which can find out those...
3
by: Jeff | last post by:
I have an asp page. Now I know this is sortof OT, but I am not sure of the forum to get an answer. I have an include on this asp page <!-- #include file=\forum\includes\consts-inc.asp --> ...
6
by: Richard | last post by:
I am new to php and am looking for some quick "get up and go" help. What would the arguments for and against be for function declarations v simple include? e.g <?php include("myfunc.php");...
4
by: Ron | last post by:
Hi all, Trying to work out a few problems in using php on my site. Partially this is a html question. I was reading a lot of the posts and it seems that some of the includes people are using are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.