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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 ================== |
|
|||
|
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. |
|
|||
|
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# |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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 ================== |
|
|||
|
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 :) |
|
|||
|
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 ================== |