I've just moved a load of sites to a new VPS and have a strange problem
with include_path. It's something that worked fine on the old server but
not on the new.
I'll try and explain it using a simple example...
In /home/kin/www/lib/Zend and also in /home/kin/www/ipb/_membadmin/Zend
there is a file called test1.php which simply echoes which folder it is in.
In /home/kin/www/ipb/_membadmin there is a file called test.php
It has one line....
require_once 'Zend/test1.php';
When you run it, as you would expect, it echoes the line from test1.php in
/home/kin/www/ipb/_membadmin/Zend
If you then rename /home/kin/www/ipb/_membadmin/Zend to something else (so
the 'include' statement can't find it), and run test.php again, it SHOULD
(I think) echo the file in /home/kin/www/lib/Zend because php include_path
is set to include /home/kin/public_html/lib (which phpinfo.php confirms).
... but it doesn't.
Why not? If there is an include path of /home/kin/public_html/lib then
surely
require_once 'Zend/test1.php';
should point to /home/kin/www/lib/Zend. It does on the old server!
I must be missing something obvious but I don't know what!
Any thoughts?
Thanks
After the diagnostic you just gave it now seems clear that something is overriding the environment path.
There has to be some line in your code where the set_include_path is getting called. Is any Zend specific code running that you haven't checked? That missing directory clearly paints the culprit as a rogue set_include_path()
Related
I wrote this code in a test.php file.
<?php
include ($_SERVER["DOCUMENT_ROOT"].'/MySite/protected/MyYouTube/google/src/Google/autoload.php');
require_once ($_SERVER["DOCUMENT_ROOT"].'/MySite/protected/MyYouTube/google/src/Google/Client.php');
require_once ($_SERVER["DOCUMENT_ROOT"].'/MySite/protected/MyYouTube/google/src/Google/Service/YouTube.php');
?>
If I go to this file like this: localhost/MySite/protected/MyYouTube/test.php it works, but if I copy the same code into my controller that is located under the same folder as test.php, I get this:
include(Google_Service.php): failed to open stream: No such file or
directory
There are no conflicts with the imports. In fact, the controller and view can be empty and still I get the same thing.
Apparently that happens when autoload.php is not actually loaded.
How is it possible that when the code is integrated into the website it throws this error?
This is what the path to my site looks like:
localhost/MySite/index.php/user/view It seems that the way I visit the file matters.
I tried several things. I tried importing the test.php into my view or my controller, and still I get the same error. I tried using Yii::app()->basePath and it gives the same problem.
The paths are correct, I have checked several times. How can I fix this?
The include path is actual server path, not site path, if you use / at the beginning, you are telling it to look in the server root.
If you know the absolute path in the server you can use it like /var/www/MySite or c:\mysiteif you don't know that then you use relative paths.
$_SERVER["DOCUMENT_ROOT"] works well with PHP 5.3, not sure if below check your version. Try removing the '/' before the MySite part, as the variable already does it for you so it might be printing like localhost//MySite, although I'm not sure if that should or shouldn't work. ]
Also the autoload php should be loaded with a require_once function, not with the include. Good luck!
I've encountered something when trying to include/require a php script that's 2 directories back. It's not really a problem as I figured out a work around, however I'd love an explanation for what's happening.
Here's the file structure:
appCode
db.php (File I'm trying to include)
studentManagement
index.php
dep
getData.php (File I'm trying to include db.php into)
I want to include appCode/db.php in studentManagement/dep/getData.php.
getdata.php is executed with ajax from index.php
When I use:
require_once("../../appCode/db.php");
It doesn't work.
The only way it works is it I change directory first:
chdir("../");
require_once("../appCode/db.php");
Why won't the first method work? I've also tried using include instead of require but it's the same. I'm testing it on mamp 3.0.4.
Any help appreciated!!
that is because when you require(),include() and their variants it's always relative to the initial php file called (in your case index.php)
in fact chdir has nothing to do with it, and this:
require_once("../appCode/db.php");
is the right way to call it.
always place your mental self (!) as if you were index.php when you require files and work with directories. your "active directory" is the one where index.php is placed. you can always verify this current working directory with getcwd() http://php.net/manual/en/function.getcwd.php
If you know your entire directory all the way from root you can use:
http://php.net/manual/en/function.set-include-path.php
Now instead of using relative paths you can always include or require files starting at your include path.
You could put this in getData.php:
set_include_path ('/homepages/www/2/yourworkingpath');//Use your own directory
require_once 'appCode/db.php';
You could also do the same thing in your other files if you need to include and it will always use your include path so you don't have to keep figuring out which directory to change to. Hopefully this helps a bit.
I have a directory/file tree as follows:
index.php
/frame/main_class.php
/frame/func/function_1.php
/frame/func/function_1.php
/cfg/config.php
//index.php
require('frame/main_class.php');
new main_class;
//frame/main_class.php
class main_class{
public function __construct(){
require('func/function_1.php');
require('func/function_2.php');
require('cfg/config.php');
}
}
The weird part is that it works. Maybe it is late and I am having a dumb-moment, but shouldn't "require('cfg/config');" be written "require('../cfg/config.php');" ?
And if it is using the root of index.php, then "require('func/function_1.php');" shouldn't work, right?
I have quadruple checked the remote server thinking that maybe there was a stray file or two... there isn't.
How can the two require statements have a different base path.....?
Does anyone know of a code snippet that could cause this to happen? I am working with some $_SERVER variables but I don't appear to be changing any of them....!?
"Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing." Explicitly saying include dirname(__FILE__) . '/path/to/file.php';avoids this confusion. – DCoder
Link to PHP Manual on "dirname".
The PHP engine will look for the requested files in the current directory, but it will also look for them in the list of paths defined in INCLUDE_PATH. If the include path lists the path from where your script is running then the given code will work. If not then it wont.
For that reason amongst others it's not a good idea to rely on the include path to resolve the path of included files. You should give the full path instead.
I'm getting really muddled up now and my brain hurts! :( lol
Root:
index.php
Includes:
cat.php
dog.php
index includes dog: include("includes/dog.php");
dog includes cat: include("cat.php");
When I run index, for cat it says:
A link to the server could not be established
Access denied for user ...
However, if I run dog, I get no problems...
I'm guessing its the path, but i've tried ./includes/cat.php to no joy...
This is because when you include a relative path, it's relative to the entry point (the first PHP file, called by the webserver).
In dog, do
include(dirname(__FILE__) . '/cat.php'); // __FILE__ is always the name of the php file it's in
It depends on where the script you are executing lies. When you execute /index.php the path of the script set to /, so all includes start from there. This means that you can find /includes/dog.php, but it's not possible to find /cats.php. Mind that, even if you are including cats.php from your /includes/dog.php script, this doesn't change the original execuption path.
When, on the other hand, you are executing /includes/dog.php, your path is set to /includes/, which is why PHP can also find cats.php.
Read Bart's comment on how to solve this.
Another way to solve this is to set the include path of files, take a look at this.
http://ve2.php.net/manual/en/function.set-include-path.php
Thanks for this nice thread.
I used bart's answer to solve this issue. But I still have one question here.
I was surprised that it worked in my mate's system even without using dirname(__FILE__) so I did little research and compared both php.ini files. I noticed there is little difference at include_path parameter in php.ini.
In my php.ini it is set to Pear directory. So I commented out just to test and to my wonder it worked. This is when I realized we need to include some folder which I dont know or comment it out so that it takes default value.
I have found a little script which looks interesting, you can download the svn from google code at
http://code.google.com/p/streetwire/source/browse/
I am trying to set it up on my xampp localhost install, and there is little documentation, here are the first two lines of the config
// Paths
define('VHOST_DIR', '/data/vhost');
define('ROOT_DIR', VHOST_DIR . '/www.streetwire.org');
I have tried the following settings but it doesnt seem to work
// Paths
define('VHOST_DIR', 'C:/xampp/htdocs');
define('ROOT_DIR', VHOST_DIR . '/streetwirescript');
For anyone who has looked at the script, im not sure where to go for the 'start' page of the script either!!
Any thoughts on what should go into Vhost_dir?
Searching through the trunk (http://www.google.com/codesearch?q=root_dir+package%3Ahttp%3A%2F%2Fstreetwire.googlecode.com&origq=vhost_dir&btnG=Search+Trunk), VHOST_DIR is only used to define ROOT_DIR. It looks like all that matters is that ROOT_DIR points to the folder that the script is located in.
It's also possible that the script doesn't run on Windows.
It looks like you need to set /docs as the document root for this to work. E.g. index.php I assume you've seen INSTALL too.
Do you have error reporting on and set to maximum? There's a devsite constant in config that I assume gives error information too. What errors are you getting?