i am using the PHP function require_once to require a file:
<?php require_once 'reviewtickets_history.php?seq='.$_GET["seq"].'&type=history' ;?>
but i am getting the error:
Warning: require_once(reviewtickets_history.php?seq=34844&type=history) [function.require-once]: failed to open stream: No such file or directory in /home/user/public_html/admin/helpdesk/reviewtickets.php on line 316
but if i remove my .php file from the URL and enter in the .php page in the brackets above (reviewtickets_history.php?seq=34844&type=history) it displays fine
Option 1: To require a file, use local path.
require_once '/path/to/reviewtickets_history.php';
This will include the full script source, and evaluate the PHP code in your current file. You cannot pass query parameters with this method.
Option 2: To require a URL, use a full URL.
require_once 'http://mydomain.com/reviewtickets_history.php?myparam=val';
This will include the output of the script, not the PHP source. You can use query parameters here.
Note that allow_url_fopen has to be enabled for this second option to work.
in require_once need to set a path on the server to the file but not a URL. For example you can't pass http://www.example.com/somefile.php but you can do /var/www/folder/somefile.php.
In case then you
require_once 'http://mydomain.com/reviewtickets_history.php'
it will include the output and not the php functions
Related
I have created a file called init.php with the following code (which is included at the top of every page)
<?php
define('APP_ROOT', 'https://example.com/myapp');
?>
I can successfully echo APP_ROOT in a traditional link, even for sub-folders, as follows
linktext
What I cannot do is use the constant with PHP Require, for neither root files or files in sub-folders, as follows
<?php require_once(APP_ROOT . '/test.php') ?>
It returns the following error message:
Fatal error: require_once(): Failed opening required 'https://example.com/myapp/test.php' (include_path='.:/usr/local/php56/pear') in...
Is it even possible to use a constant with require or include statements? If so, would appreciate any direction
If your code is running on multiple servers with different environments (locations from where your scripts run) the following idea may be useful to you:
Do not give absolute path to include files on your server.
Dynamically calculate the full path (absolute path)
Hints:
Use a combination of dirname(FILE) and subsequent calls to itself until you reach to the home of your '/index.php'. Then, attach this variable (that contains the path) to your included files.
One typical example is:
<?php
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__.'/config.php');
?>
https://www.php.net/manual/en/function.require-once.php
Since moving my website from local to online , I have a path problem using require.
I have pages that call bootstrap.php like this:
require 'inc/bootstrap.php';
And boostrap.php looks like this:
<?php
spl_autoload_register('app_autoload');
function app_autoload($class){
require "class/$class.php";
}
This worked well in local.
Now, online, I get the following message:
Warning: require_once(/home/website/www/class/functions.php): failed to open stream: No such file or directory in /home/website/www/inc/bootstrap.php on line 6
Fatal error: require_once(): Failed opening required '/home/website/www/class/functions.php' (include_path='.:/usr/share/php:/usr/share/pear')
in /home/website/www/class/functions.php on line 6
So I thought, I should put an absolute path in boostrap.php like this:
<?php
spl_autoload_register('app_autoload');
function app_autoload($class){
$path = $_SERVER['DOCUMENT_ROOT'] . "class/$class.php";
require_once "$path";
}
But I get the exact same error.
I dont understand why it is still looking in the /home/website/www/inc/bootstrap.php and not following the absolute path which is /home/website/www/class/functions.php ?
EDIT:
After testing the different solutions using absolute pathes, I am still getting the error that "No such file or directory in /home/website/www/bootstrap.php: so it is still looking in the file instead of directory.
Could it be because I am using a double require? I first require boostrap.php from description.php (which works fine) and then I require class.php from this boostrap.php (which doesent take the absolute path but the path corresponding to the file boostrap.php ?
ANSWER:
Ok it finally works with this configuration:
First file using require:
require_once(dirname(__FILE__). '/inc/bootstrap.php');
and boostrap.php:
require_once(dirname(__FILE__). "/../class/$class.php");
As it was said in comments, it is safe to use absolute path instead of 'inc/...', cause it is relative to current class execution path, i.e.:
define ('BASE_PATH', dirname(__FILE__).'/../');
Then use BASE_PATH.{your_path} to access files.
I suggest to use require_once to avoid multiple inclusions.
I'm trying to implement Google APIs in PHP on WAMP, but I can't even manage to include the files.
My code is the following :
<?php
require_once "./vendor/google/apiclient/src/Google/Client.php";
Client.php
require_once 'Google/Auth/AssertionCredentials.php';
require_once 'Google/Cache/File.php';
require_once 'Google/Cache/Memcache.php';
....
My error :
Warning: require_once(Google/Auth/AssertionCredentials.php): failed to
open stream: No such file or directory in
C:\wamp\www\mysite\vendor\google\apiclient\src\Google\Client.php on
line 18
How should I use include path, and similar commands to get Client.php to find what it wants ?
There are multiple ways so that the file can be found. These include the absolute path or also using setting the include path. The include path can be set via:
php.ini
.htaccess
ini_set function
set_include_path function
Please goole the one most appropriate for your use
I solved my problem using Composer.
Somehow, I cannot include the file I want, neither using require_once, nor include. The file I want to include is in the same directory as the file in which I want to include it. I tried every combination, but still does not work.
The error I get trying to use require_once
Warning: require_once(D:\PROJEKAT\wamp\www\Eif\db_connect.php): failed to open stream: No such file or directory in D:\PROJEKAT\wamp\www\Eif\create_user.php
I first tried with
require_once '/db_connect.php'
then I used
require_once '\db_connect.php' cause I realized that I have to use \ in windows
Then tried
require_once __DIR__ . '\db_connect.php'
And lastly
require_once $_SERVER['DOCUMENT_ROOT'] . '/Eif/db_connect.php';
I think that the path I'm using is good, but it keeps using it on the wrong way.
What is the problem?
Thanks
Quick answer
You should be able to do this:
require_once 'db_connect.php';
Furthermore information
require, require_once, include and include_once are all the same with one exception: require will emit a fatal error on failure where as include will emit warning.
The other thing to bare in mind these actually search through the php ini include_path directive, so when you attempt to include another file php will search through those directories in order of the first directive.
By default this directive usually looks like: .:/usr/share/php:/usr/share/pear. This could be changed by a number of functions, even the php.ini which you would want to be aware of:
set_include_path - Set the include paths directive;
get_include_path - Show the current include paths;
getcwd - Show the current working directory;
chdir - Change the current working directory;
NOTE if using set_include_path you are not adding to the list, but overriding it's value; so it would be wise to know the correct way of setting this is:
set_include_path(get_include_path() . PATH_SEPERATOR . '/path/to/new/location');
If the file you want to include are in the same directory this should work:
require_once 'db_connect.php'
There has to be some mistake in the way you are trying to reach to the path.
You can use following php functions to debug it.
<?php
print_r(get_included_files()); // Write this above the file that you are trying to include
set_include_path('path_to_your_directory');
// This will set the directory for once in<br /> your page after setting the path use require_once with the file name straight away
?>
I have inside my div a php require to
require('inc/coreturni/list.php');
that requires again a db connection
require('inc/connect.inc.php');
and they both work when I load the page.
but if I try to refresh the div, by refreshing the first include file after a click event, with jquery load()
I got the following path error:
Warning: require(inc/connect.inc.php): failed to open stream: No such file or directory in C:\xampp\htdocs\taximat\inc\coreturni\list.inc.php on line 2
Fatal error: require(): Failed opening required 'inc/connect.inc.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\taximat\inc\coreturni\list.inc.php on line 2
why the path should be different? how could i fix it?
Your error explains it all:
Warning: require(inc/connect.inc.php): failed to open stream: No such
file or directory in
C:\xampp\htdocs\taximat\inc\coreturni\list.inc.php on line 2
Fatal error: require(): Failed opening required 'inc/connect.inc.php'
(include_path='.;C:\xampp\php\PEAR') in
C:\xampp\htdocs\taximat\inc\coreturni\list.inc.php on line 2
Notice how it says it can’t find inc/connect.inc.php in this directory:
C:\xampp\htdocs\taximat\inc\coreturni
It’s because your require statements are referring to relative paths. So when the file list.inc.php is loaded, the require is assuming that inc/connect.inc.php is located in C:\xampp\htdocs\taximat\inc\coreturni. Which is why it is failing.
You should set a base path in your app like so. Just a note that I am unclear on forward-slash or back-slash syntax in a case like this since I work mainly on Mac & Unix systems and the error shows windows paths, but the PHP is showing Unix paths.
$BASE_PATH='/xampp/htdocs/taximat/inc/coreturni/';
And the require all files like this:
require($BASE_PATH . 'inc/coreturni/list.php');
And like this:
require($BASE_PATH . 'inc/connect.inc.php');
That way no matter where the files are called from, the require will use an absolute path to the file. And since $BASE_PATH is a one-time variable you set you can just set it in your main config file and not worry about it. To make the code portable, just change $BASE_PATH based on system setups.
When the page is displayed for the first time, your paths are like this:
page.php
inc/
coreturni/
list.php
connect.php
In other words, all require statements will be relative as seen from page.php.
However, when you request list.php directly using AJAX, it looks more like this:
list.php
../
connect.php
Now, the require should have been for ../connect.php.
Solution
One way to solve this is by calculating the absolute path based on the file currently being processed (i.e. list.php) like this:
require dirname(__DIR__) . '/connect.php';
The dirname(__DIR__) construct will always yield the directory on your file system that's one higher up than the current script is in.