Bluehost.com Web Hosting $6.95

include file path problem

This is a discussion on include file path problem within the PHP Language forums, part of the PHP Programming Forums category; Hello, I built a development version of a live website on my hosted account. However the development version is having ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-20-2008
Raheem
 
Posts: n/a
Default include file path problem

Hello,

I built a development version of a live website on my hosted account.
However the development version is having problems with finding
include files. After troubleshooting I was able to resolve the issue
by removing the ../ from the beginning of the include file path. But
now I am looking at making this modification hundreds of times in all
kinds of files. I'd like to keep the dev env similar to the live env
so I can port changes easily. Below is an example of working and non-
working include file definitions:

working - include_once("dblib/db_con.php");
non-working - include_once("../dblib/db_con.php");

But I dont understand why this would work in one server and not the
other. Is there some kind of setting in htaccess or php.ini that would
affect this? The dev env is running php 5.2.6. Appreciate your help.

- Raheem
Reply With Quote
  #2 (permalink)  
Old 08-20-2008
Jerry Stuckle
 
Posts: n/a
Default Re: include file path problem

Raheem wrote:
> Hello,
>
> I built a development version of a live website on my hosted account.
> However the development version is having problems with finding
> include files. After troubleshooting I was able to resolve the issue
> by removing the ../ from the beginning of the include file path. But
> now I am looking at making this modification hundreds of times in all
> kinds of files. I'd like to keep the dev env similar to the live env
> so I can port changes easily. Below is an example of working and non-
> working include file definitions:
>
> working - include_once("dblib/db_con.php");
> non-working - include_once("../dblib/db_con.php");
>
> But I dont understand why this would work in one server and not the
> other. Is there some kind of setting in htaccess or php.ini that would
> affect this? The dev env is running php 5.2.6. Appreciate your help.
>
> - Raheem
>


The first one looks in the current directory. The second one looks in
one directory lower. It looks like your file setup is different between
your test and production servers.

BTW - I always use absolute paths instead of relative. That way if I
move a file that includes something else, it still works, i.e.

include ($_SERVER['DOCUMENT_ROOT'] . '/dblib/db_con.php');

This looks for the file in the dblib of the web root directory.

P.S. If you MUST post to multiple newsgroups, please crosspost. Don't
multipost.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Reply With Quote
  #3 (permalink)  
Old 08-20-2008
sheldonlg
 
Posts: n/a
Default Re: include file path problem

Jerry Stuckle wrote:
> Raheem wrote:
>> Hello,
>>
>> I built a development version of a live website on my hosted account.
>> However the development version is having problems with finding
>> include files. After troubleshooting I was able to resolve the issue
>> by removing the ../ from the beginning of the include file path. But
>> now I am looking at making this modification hundreds of times in all
>> kinds of files. I'd like to keep the dev env similar to the live env
>> so I can port changes easily. Below is an example of working and non-
>> working include file definitions:
>>
>> working - include_once("dblib/db_con.php");
>> non-working - include_once("../dblib/db_con.php");
>>
>> But I dont understand why this would work in one server and not the
>> other. Is there some kind of setting in htaccess or php.ini that would
>> affect this? The dev env is running php 5.2.6. Appreciate your help.
>>
>> - Raheem
>>

>
> The first one looks in the current directory. The second one looks in
> one directory lower. It looks like your file setup is different between
> your test and production servers.
>
> BTW - I always use absolute paths instead of relative. That way if I
> move a file that includes something else, it still works, i.e.
>
> include ($_SERVER['DOCUMENT_ROOT'] . '/dblib/db_con.php');
>
> This looks for the file in the dblib of the web root directory.
>
> P.S. If you MUST post to multiple newsgroups, please crosspost. Don't
> multipost.
>



Huh?

The first one looks for the dblib directory in the current directory and
then looks for dbcon.php. It is equivalent to ./dblib/db_con.php.

The second one looks for a directory, dblib, which is on the same level
as the current directory. IOW, it goes up one level and then tries to
find the directory dblib and in that directory looks for db_con.php.

Apparently, since the first one worked and the second didn't, the dblib
directory must be in the current directory.
Reply With Quote
  #4 (permalink)  
Old 08-20-2008
Raheem
 
Posts: n/a
Default Re: include file path problem

Thanks for your help guys! I have a better understanding now. The
solution was to use symlinks. Full solution listed in
http://groups.google.com/group/alt.p...d7f522e?hl=en#
Reply With Quote
  #5 (permalink)  
Old 08-20-2008
Jensen Somers
 
Posts: n/a
Default Re: include file path problem

Raheem wrote:
> Thanks for your help guys! I have a better understanding now. The
> solution was to use symlinks. Full solution listed in
> http://groups.google.com/group/alt.p...d7f522e?hl=en#


Yet the problem still exists and you merely "hacked" together a solution
which will fail if one day your website moves to another server.

The problem is that the directory structure on your development and live
platform is different. Fix that, and you won't have any other problems
now and in the future.

--
Jensen Somers <http://jsomers.eu>
Email: -http:// +jensen@

"Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe trying to
produce bigger and better idiots. So far, the Universe is winning."
- Rick Cook, The Wizardry Compiled
Reply With Quote
  #6 (permalink)  
Old 08-20-2008
Raheem
 
Posts: n/a
Default Re: include file path problem

True, if the live site needs to be moved, its going to be a major
problem. But due to contractual limitations, my work cannot change the
client's site structure etc. That is why I need the dev env to be
exact as the live env - so that I can port changes.
Reply With Quote
  #7 (permalink)  
Old 08-20-2008
Jensen Somers
 
Posts: n/a
Default Re: include file path problem

Raheem wrote:
> True, if the live site needs to be moved, its going to be a major
> problem. But due to contractual limitations, my work cannot change the
> client's site structure etc. That is why I need the dev env to be
> exact as the live env - so that I can port changes.


But you still have an error somewhere in the directory structure.

If you are using include_once("../dblib/db_con.php"); on your
development platform it means the directory structure is as follows
(taking 'my_file.php' as an example file):

root/
+ dblib/
- db_con.php
+ my_folder
- my_file.php

This works because you go up 1 folder (to root/), into the 'dblib'
folder and including the file 'db_con.php'. Now, if you are saying only
include_once('dblib/db_con.php'); works on your live environment this
means the folder structure is as follows:

root/
+ my_folder
- my_file.php
+ dblib/
- db_con.php

Thus you have an error.

Now, there is one other alternative which means your development
environment has different settings as your live environment and you
should use full path's instead. (As suggested earlier.) If you are using
an 'index.php' file and including everything in that file whilst
including other files in already included files your folder structure
must be starting from your 'index.php' file.

Example:

root/
- index.php
+ dblib/
- db_con.php
+ my_folder/
- my_file.php

index.php:
<?php
include_once('my_folder/my_file.php');
?>

my_file.php:
<?php
// Include database connection.
include_once('../dblib/db_con.php');
?>

The above example will not work because the path is incorrect. The
correct solution should be:

my_file.php:
<?php
include_once('dblib/db_con.php');
?>

Because: you are already including 'my_file.php' in 'index.php' making
the contents of 'my_file.php' to be inserted into 'index.php'. Thus
creating:

index.php:
<?php
// Include database connection
include_once('../db_lib/db_con.php');
// That is an incorrect path, you are going outside the 'root' folder.
?>

I know from experience that this might work or not work on certain web
server installations, which makes using full paths a lot easier to work
with since you can't go wrong.

- Jensen

--
Jensen Somers <http://jsomers.eu>
Email: -http:// +jensen@

"Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe trying to
produce bigger and better idiots. So far, the Universe is winning."
- Rick Cook, The Wizardry Compiled
Reply With Quote
  #8 (permalink)  
Old 08-20-2008
Jerry Stuckle
 
Posts: n/a
Default Re: include file path problem

sheldonlg wrote:
> Jerry Stuckle wrote:
>> Raheem wrote:
>>> Hello,
>>>
>>> I built a development version of a live website on my hosted account.
>>> However the development version is having problems with finding
>>> include files. After troubleshooting I was able to resolve the issue
>>> by removing the ../ from the beginning of the include file path. But
>>> now I am looking at making this modification hundreds of times in all
>>> kinds of files. I'd like to keep the dev env similar to the live env
>>> so I can port changes easily. Below is an example of working and non-
>>> working include file definitions:
>>>
>>> working - include_once("dblib/db_con.php");
>>> non-working - include_once("../dblib/db_con.php");
>>>
>>> But I dont understand why this would work in one server and not the
>>> other. Is there some kind of setting in htaccess or php.ini that would
>>> affect this? The dev env is running php 5.2.6. Appreciate your help.
>>>
>>> - Raheem
>>>

>>
>> The first one looks in the current directory. The second one looks in
>> one directory lower. It looks like your file setup is different
>> between your test and production servers.
>>
>> BTW - I always use absolute paths instead of relative. That way if I
>> move a file that includes something else, it still works, i.e.
>>
>> include ($_SERVER['DOCUMENT_ROOT'] . '/dblib/db_con.php');
>>
>> This looks for the file in the dblib of the web root directory.
>>
>> P.S. If you MUST post to multiple newsgroups, please crosspost. Don't
>> multipost.
>>

>
>
> Huh?
>
> The first one looks for the dblib directory in the current directory and
> then looks for dbcon.php. It is equivalent to ./dblib/db_con.php.
>
> The second one looks for a directory, dblib, which is on the same level
> as the current directory. IOW, it goes up one level and then tries to
> find the directory dblib and in that directory looks for db_con.php.
>
> Apparently, since the first one worked and the second didn't, the dblib
> directory must be in the current directory.
>


Which is basically what I said.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Reply With Quote
  #9 (permalink)  
Old 08-21-2008
Motosauro
 
Posts: n/a
Default Re: include file path problem

Raheem ha scritto:
> Hello,
>
> I built a development version of a live website on my hosted account.
> However the development version is having problems with finding
> include files. After troubleshooting I was able to resolve the issue
> by removing the ../ from the beginning of the include file path. But
> now I am looking at making this modification hundreds of times in all
> kinds of files. I'd like to keep the dev env similar to the live env
> so I can port changes easily. Below is an example of working and non-
> working include file definitions:
>
> working - include_once("dblib/db_con.php");
> non-working - include_once("../dblib/db_con.php");
>
> But I dont understand why this would work in one server and not the
> other. Is there some kind of setting in htaccess or php.ini that would
> affect this? The dev env is running php 5.2.6. Appreciate your help.
>
> - Raheem



A nice solution I use for keeping includes out of the way is to set two
directives (they can be set in both php.ini and vhost file)
Say your site has its root in /var/www/sitename/htdocs
You create a dir which is: /var/www/sitename/includes
then you set
open_basedir('/var/www/sitename')
And
include_path='/var/www/sitename/includes';

So your includes cannot by any means be served on their own, since they
are out of the directory tree served by your webserver
Just a little bit safer
:)
Reply With Quote
  #10 (permalink)  
Old 08-21-2008
Jerry Stuckle
 
Posts: n/a
Default Re: include file path problem

Motosauro wrote:
> Raheem ha scritto:
>> Hello,
>>
>> I built a development version of a live website on my hosted account.
>> However the development version is having problems with finding
>> include files. After troubleshooting I was able to resolve the issue
>> by removing the ../ from the beginning of the include file path. But
>> now I am looking at making this modification hundreds of times in all
>> kinds of files. I'd like to keep the dev env similar to the live env
>> so I can port changes easily. Below is an example of working and non-
>> working include file definitions:
>>
>> working - include_once("dblib/db_con.php");
>> non-working - include_once("../dblib/db_con.php");
>>
>> But I dont understand why this would work in one server and not the
>> other. Is there some kind of setting in htaccess or php.ini that would
>> affect this? The dev env is running php 5.2.6. Appreciate your help.
>>
>> - Raheem

>
>
> A nice solution I use for keeping includes out of the way is to set two
> directives (they can be set in both php.ini and vhost file)
> Say your site has its root in /var/www/sitename/htdocs
> You create a dir which is: /var/www/sitename/includes
> then you set
> open_basedir('/var/www/sitename')
> And
> include_path='/var/www/sitename/includes';
>
> So your includes cannot by any means be served on their own, since they
> are out of the directory tree served by your webserver
> Just a little bit safer
> :)
>


You don't even need access to the php.ini file (not available at most
shared hosts). PHP can access anything on the file system your host
allows you to - and the better ones will allow you to access one level
below the web root. So you can use something like:

include ($_SERVER['DOCUMENT_ROOT'] . '/../include/myinclude.php');

Assuming your document root points to /var/www/example/html, this gets
the file from /var/www/example/include.

And if your hosting company doesn't give you access, you're probably
better off getting another hosting company, anyway.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:21 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0