Good morning at all! I have an issue that I don't understand.I've XAMPP
localhost files where include function doesn't work in one file, while
in other files works without problems.I'm running Chrome browser. I've
already tried to clear browser cache, but issue still exists. Why?
Include function works in this pages:
"index.php",
"blog.php",
"history.php"
Include function doesn't work only on this page:
contacts.php
The code is here:
<?php include('include/menu.php'); ?>
You need your absolute path to the include file correct by prefixing with $_SERVER['DOCUMENT_ROOT'];
In all your includes() do :
include($_SERVER['DOCUMENT_ROOT'].'/include/menu.php');
it would help to know two things: 1) where are the files located in? 2) what is the error message?
maybe the solution is simple since there is only the lack of the correct path:
include(__DIR__."/blog.php");
etc.
__DIR__ contains the path of the current script and is very useful to go up/down the directories based on it.
I've solved issue. I copied another existent page such as index.php with menu and footer correctly included, and I changed the body content and now it works.
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 have decided to swap my standard html navigation for a php include, i have a multi deirectory site so have had to include the file using ../ to move up the directories where applicable.
When using the navigation from my root folder it works fine but when i then try and use the same navigation in a subdirectory file my file paths get added onto the end of the current path for example.
http://example.co.uk/catagoryOne/catagoryTwo/index.php
Becomes:
http://example.co.uk/catagoryOne/catagoryTwo/catagoryOne/catagoryThree/index.php
Where i need it to be:
http://example.co.uk/catagoryOne/catagoryThree/index.php
Could anyone tell me why this is happening?
Many thanks in advance
P.S. Apologies if this is not clear please let me know if anything requires further clarification.
Wouldn't this be solved via .htaccess as it is to do with url?
Read more on .htaccess here
To fix this problem i used
<li><a href="http://<?php echo $_SERVER['HTTP_HOST']; ?>/catagoryOne/catagoryThree/index.php"></li>
within navigation.php where applicable.
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'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()
I currently working on a PHP project. I copy the project file to my local box. It runs fine except one thing.
Here is the folder hierarchy:
root/index.php
root/event/admin/list.php
root/event/admin/functions.php
In the index.php, there is a line:
<?php include ("event/admin/list.php"); ?>
which should include the list.php
However in the list.php, there is a line:
<?php include_once "event/admin/functions.php";?>
Since the list.php is not in the root directory, event/admin/functions.php did not get call and my local index.php fail to load this part.
But the production is working fine.
Does anyone know what happened? Is that a way to setup include/include_once always use ROOT directory without using something like $_SERVER["DOCUMENT_ROOT"]? Thanks a lot.
It is a good idea to use $_SERVER['DOCUMENT_ROOT'], in my opinion. You can do so like this:
include_once($_SERVER['DOCUMENT_ROOT'] . "/path/to/file.php");
However, try replacing he code in list.php with this:
<?php include_once "../../event/admin/functions.php";?>
This is a known issue relating to relative paths. Thus, DOCUMENT_ROOT is preferable. Alternatively, you can edit include_path.
Set the include path to whatever is useful for your probject.
This is a common issue related to relative paths. You can either include your files from an absolute path, or modify your include_path in php.ini to use your doc root, and specify files relative to there.
Compare the include path configuration between the two machines.