I'm still a bit new to PHP and stumbled across a weird error I can't fix.
I have different util PHP files for different purposes, such as getting user data or basic util functions.
But I can't figure out why PHP does sometimes not find the path when using the require function.
In particular this example:
print 'Huh';
ob_start();
require "/api/connection.php";
ob_end_clean();
HTML shows a working link redirecting to the page, but PHP still can't find the connection file.
Here is my file tree:
So why is that and how can I fix it? Does PHP have another way of linking files or is it something else?
Thanks in Advance
PHP paths are relative to the root of the filesystem, not the document root. Use $_SERVER['DOCUMENT_ROOT'] to get the latter.
require $_SERVER['DOCUMENT_ROOT'] . "/api/connection.php";
Related
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.
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 am developing a website on a local test environment. From time to time I need to import classes or functions into my php pages. At first I used relative paths to import files, but for some (unexplicable) reason the PHP couldn't find those files. So I have thought it would be necessary use the following:
<?php require_once($_SERVER['DOCUMENT_ROOT'] .'/mywebsite/inc/libs/functions.php'); ?>
The problem is when I have to upload all the pages to my remote webserver, the PHP interpreter won't find functions.php in that position because there is no mywebsite subfolder , on the other hand I can't get rid of mywebsite subfolder, because that would leave me with http://localhost/inc/libs/functions.php which leads nowhere.
So that basically means I will have to manually readjust the path to make everything work. My question is: is there a way for PHP to detect the exact folder of my website so that I don't have to change paths everytime I need to upload a php file to my webserver?
What is the reason behind impossibility of use relative path?
<?php require_once(dirname(__FILE__).'/inc/libs/functions.php'); ?>
Set the base folder with all the files you want as an include path in php.ini.
That way, when you use include () or require (), it will automatically look in the included path.
www.php.net/manual/en/ini.core.php#ini.include-path
I have a PHP application I'm trying to include, but it's causing problems because I'm trying to include its files from a subdirectory. For instance, here's the directory structure (more or less) I'm trying to work with:
/site
/scripts
/local
some_script.php
lib_file_a.php
lib_file_b.php
lib_load.php
lib_load.php has an include command for files lib_file_a.php and lib_file_b.php (relative path).
What I want to accomplish is to include lib_load.php from some_script.php, but there's a problem with the way I'm including lib_load.php. At first I tried require('../../../lib_load.php');.
As you would expect I saw the error, "Failed opening required 'C:\xampp\htdocs\site\scripts\local\lib_file_a.php'". As you can see, PHP is trying to include a file that does not exist. I found a Stack Overflow post that told me about using an absolute path (link), but this did not help. I next tried this code:
require(realpath(dirname(__FILE__) . '../../../' . 'lib_load.php'));
However, this time, the same error comes back. What am I doing wrong?
EDIT: I realized that the issue was actually in the library loader, which turned out to be including files based on an absolute path, not a relative path. (I should have read more carefully.) I resolved the issue by changing the path used by the library. This question should be closed if a mod sees this request.
To include lib load
require $_SERVER['DOCUMENT_ROOT'] . '/scripts/lib_load.php';
inside of lib load
require dirname(__FILE__) . '/lib_file_a.php';
if I got your problem right
Try this:
require dirname(__FILE__) . '/../../../lib_load.php');
Or if you're using PHP 5.3 or higher use this instead (it looks nicer):
require __DIR__ . '/../../../lib_load.php');
I am working on a php project and I am having problems with including files.
I have a php script which is located at myapp/reports/index.php. When a form is submitted it performs an ajax post to another phpscript located at myapp/reports/phpHander/submit.php.
Submit.php then has to include a php script which is used to send an email. This is done to ensure that same code can be used over and over again without it needing to be typed for each time it is need. This php script is located at ../../administrator/classes/send.php.
Up to this point is working fine however the send.php script includes another file to get app config settings which is located in administrator/appConfig.php. The problem is this appConfig.php isn't being found even if I put in the full web address of http://localhost/myapp/administrator/appConfig.php.
What am I doing wrong. I am using the php include function to do this and its working for everything else but it seems to have a problem then including another script from a different location.
Thanks for any help you can provide.
http://localhost/myapp/administrator/appConfig.php is only URL path.
You need absolute filepath like C:/wamp/www/myapp/administrator/appConfig.php (Windows) or /var/www/myapp/administrator/appConfig.php (Linux)
Anyways best way is make a file "dirs.php" in your root application directory with constant:
define('ROOT_DIR', dirname(__FILE__));
where dirname(__FILE__) will be absolute path to your app directory.
With this knowledge you can include files in this way:
myapp/reports/index.php:
require_once('../../dirs.php');
include(ROOT_DIR . '/administrator/appConfig.php');
myapp/reports/phpHander/submit.php.:
require_once('../../../dirs.php');
include(ROOT_DIR . '/administrator/appConfig.php');
When you include another PHP script, all the paths are relative to the calling script. So, it sounds as though your script is at myapp/reports/phpHander/submit.php and includes ../../administrator/classes/send.php, which then includes another script in that same directory. In this case, you need to use the path "../../administrator/appConfig.php". Alternatively, you could use absolute paths relative to the filesystem's root.