Reputation: 5907
In my site, i have a place that says:
include('../tpls/header_home.php');
and it works. Now i am moving the site to another server, where i get the error:
Warning: include(../tpls/header_home.php) [function.include]: failed to open stream: No such file or directory in /home/usern/public_html/site/tpls/static/home.php on line 4
now if I replace the line with this one, it works:
include('site/tpls/header_home.php');
I could change it, but i don't know how many more places I have of broken paths all around, not only in templates, but files, uploads etc.
I would rather fix the problem generally from a setting or something else I am missing. What do I do?
HINT:
Old working server gives me CWD:
/home/ortho/public_html/site/lib
New server that is problematic gives me CWD:
/home/orthosho/public_html
why?
STRUCTURE:
/home/usern/public_html/site/tpls/static/home.php
/home/usern/public_html/site/tpls/header_home.php
PHP old server: 5.2.17
PHP new server: 5.3.8
maybe that is causing problems?
Upvotes: 0
Views: 417
Reputation: 5371
Sometimes it helps to use dirname so its really relative to the current file. this is what I use if I make cronjobs, and its more "portable" this way
<?php
include dirname(__FILE__)."/../tpls/header_home.php";
Upvotes: 0
Reputation: 6071
Given your new information, the problem is the following:
Old server => ~/public_html/site/lib
New server => ~/public_html
Why? Well, it's because you've set it that way. How do you access the previous website? yourdomain.com/site/
, or just yourdomain.com
? If it's the first one, then it's bound to be that you've got different web roots.
Now, what you're trying to include is this:
Old server => ~/public_html/site/tpls/...
New server => ~/tpls/
That doesn't quite look right does it?
My guess is that you'd need to alter the document root in httpd.conf
in some way.
Upvotes: 0
Reputation: 9520
You must have config file, which will have the abosolute path to your site, ofc it will be relative (no need to be hardcoded url) and then use it in all include calls.
Let me explain you, lets say you have a folder in your site called config and in in file called config.php. If you want you could make config class that will hold all your important variables. One of them should be
$ROOT_PATH = str_replace( '\\', '/', dirname( dirname( __FILE__ ) ) ) . '/';
That will be root path relative to your project. Then you can simply call this variable elsewhere when you include something:
require_once $ROOT_PATH . 'blabla/test.php';
Alternative to fix your broken code is to redefine include path with set_include_path()
http://php.net/manual/en/function.set-include-path.php
But for future, learn to code smart :)
Upvotes: 0
Reputation: 622
Well, you basically answered your own question.
Create a settings file where you keep all of these specific paths as variables or otherwise, and include that file wherever you need to reference the paths. That's generally how it works.
settings.inc.php:
$_CONFIG['BASE_DIR'] = "/var/www/yoursite.com/";
some_file.php:
require 'settings.inc.php';
include $_CONFIG['BASE_DIR'] . "some_dependency.php";
I assume you are trying to fix the issue having already made the entire system, without a common settings file, in which case I don't have an answer. You should really centralise things as soon as you start coding to avoid these kinds of issues.
Upvotes: 1