I'm having problem with include() and require_once() this is what I have on my script on report-publisher file that resides here :
C:\xampp\htdocs\checklists\reports\report-publishers.php
now i want to include this file:
<?php include("images/report-publisher-completed.php");?>
and the directory of that script is here:
C:\xampp\htdocs\checklists\reports\images\report-publisher-completed.php
it works the problem is in this report-publisher-completed.php i have the following:
require_once("../../includes/database.php");
here is the location of that file: C:\xampp\htdocs\checklists\includes\database.php
so when i run the code I get the following error:
( ! ) Warning: require_once(../../includes/database.php): failed to
open stream: No such file or directory in
C:\xampp\htdocs\checklists\reports\images\report-publisher-completed.php
on line 3
( ! ) Fatal error: require_once(): Failed opening required
'../../includes/database.php' (include_path='C:\xampp\php\PEAR') in
C:\xampp\htdocs\checklists\reports\images\report-publisher-completed.php
on line 3
How can I fix this issue?
Thanks!
Try like this
require_once(dirname(__FILE__).'/../../includes/database.php');
The safer way to include a file is to use a relative path from the current script, using the constant __dir__, like this :
include __dir__ . '/includes/database.php';
So, for your case, probably :
include __dir__ . '/../../includes/database.php';
Try this
require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/database.php";
Instead of
require_once("../../includes/database.php");
Related
I get this error when I try to call product_class.php in index.php. My product_class.php works fine, but when I try to add it to index this error show up:
Warning: require_once(../Includes/config/general.php): failed to open stream: No such file or directory in D:\Programming\Web\xampp\htdocs\android-api\product\product_class.php on line 2
Fatal error: require_once(): Failed opening required '../Includes/config/general.php' (include_path='D:\Programming\Web\xampp\php\PEAR') in D:\Programming\Web\xampp\htdocs\android-api\product\product_class.php on line 2
here my code at index.php
<?php
require_once('product/product_class.php');
require_once('Includes/config/general.php');
require_once INCLUDES_DIR.'/database.php';
?>
Make sure you specify the correct file path. If the index.php file and the product or Includes folder are in the same folder, try this:
require_once __DIR__ . '/product/product_class.php';
require_once __DIR__ . '/Includes/config/general.php';
When using PHP require_once it's not finding the specified directory.
Made sure multiple times this directory does exist!
Also tried this with include. What's the problem here?
File structure:
http://prntscr.com/hut7f0
Code:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
class main
{
public static
$test;
}
require_once '/engine/test.class.php';
main::$test = new Test();
?>
Error:
Warning: require_once(/engine/test.class.php): failed to open stream: No such file or directory in C:\xampp\htdocs\includes\loader.php on line 14
Fatal error: require_once(): Failed opening required '/engine/test.class.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\includes\loader.php on line 14
The problem might be due to usage of an absolute path (path starting with /). PHP then tries to find the file at the root of your filesystem. I guess you want to use a relative path here:
require_once 'engine/test.class.php';
You can use the __FILE__ constant to get the base path of your current file:
require_once dirname(__FILE__) . '/engine/test.class.php';
I am calling the function require_once '../autoload.php'; but I get the error
Warning: require_once(../autoload.php): failed to open stream: No such file or directory in C:\xampp\htdocs\Api\Autotask\vendor\test.php on line 2
Fatal error: require_once(): Failed opening required '../autoload.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\Api\Autotask\vendor\test.php on line 2
What am I doing wrong?
To avoid this type of problems, and for best practice, i advice to use the root path:
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
The code above will assign to $root your realpath on the server.
After that just use the require/include etc according to your localhost/webserver root :
require_once($root/Api/Autotask/vendor/autoload.php)
For example, if your file is located in websiterootfolder/php/nice_folder
Your code will be :
require_once($root/php/nice_folder/autoload.php)
By using this practice, you can include any file from whatever position and any way you want, dynamic or not
What I have done:
) Downloaded Swift-4.1.5.tar
) Extracted it
) Uploaded to my host in /public_html/domain/lib using FileZilla
) Made a new script using the code below.
) Opened it in the Browser and I got the following error below
So my question is, how am I getting the error that the file doesn't exist if It's 100% there? Thanks!
<?php
require_once '/public_html/domain/lib/swift_required.php';
?>
Warning: require_once(/public_html/domain/lib/swift_required.php) [function.require-once]: failed to open stream: No such file or directory in /home/myuser/public_html/domain/email.php on line 5
Fatal error: require_once() [function.require]: Failed opening required '/public_html/domain/lib/swift_required.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/myuser/public_html/domain/email.php on line 5
Here's a screenshot of my dir from FileZilla.
http://i.imgur.com/Zsy8y.jpg
This is wrong:
// this is absolute path, just like /home/user or /var/www
require_once '/public_html/domain/lib/swift_required.php';
Use instead:
// this is relative path to the current file
require_once 'public_html/domain/lib/swift_required.php'; //notice: no '/' in the beggining
OR:
// so use this one if you know the whole path to the file
require_once ABSOLUTE_PATH_TO . 'public_html/domain/lib/swift_required.php';
OR:
// or use this one if you don't know the whole path
// or if the path will change (dev machine and production machine)
require_once dirname(__FILE__) . RELATIVE_PATH_TO . 'public_html/domain/lib/swift_required.php';
I keep getting an error when I try to include simpletest in my include_path:
<?
set_include_path(get_include_path() . PATH_SEPARATOR . realpath(basename(dirname(__FILE__)) . '/../../'));
include 'simpletest/autorun.php';
returns:
.:/usr/lib/php:/Users/kristiannissen/Documents/php
Warning: require_once(/Users/kristiannissen/Documents/php/simpletest/arguments.php): failed to open stream: No such file or directory in /Users/kristiannissen/Documents/php/simpletest/reporter.php on line 13
Fatal error: require_once(): Failed opening required '/Users/kristiannissen/Documents/php/simpletest/arguments.php' (include_path='.:/usr/lib/php:/Users/kristiannissen/Documents/php') in /Users/kristiannissen/Documents/php/simpletest/reporter.php on line 13
personally your view pages are the first executed, such as index.php and view pages should always be in the root of your html.
so within index.php you can do:
define("BASE_PATH",str_replace("\\","/",dirname(__FILE__)));
so now BASE_PATH would be equal to: /Users/kristiannissen/Documents/php/simpletest
So if you want to phpdirectory in your include path you should use dirname() to go UP a directory:
//Get an array of your include paths
$include_parts = explode(PATH_SEPARATOR,get_include_path());
//Extend the paths
$include_parts[] = dirname(dirname(BASE_PATH)); //this is ../../
//recompile the paths and set them
set_include_path(implode(PATH_SEPARATOR,$include_parts));
this is the safe way to accomplish what your trying to do.
There is an alpha2-release on sourceforge that has the arguments.php-require commented out. I guess it isn't needed anymore.