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';
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';
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 have the following structure
otsg
> class
> authentication.php
> database.php
> user.php
> include
> config.inc.php
> encryption.php
> include.php
> session.php
> index.php
> registration.php
include.php file has the following
ini_set('display_errors', 1);
error_reporting(E_ALL);
ini_set('include_path',ini_get('include_path').':/Applications/MAMP/htdocs/otsg/:');
require_once 'config.inc.php';
require_once '../class/database.php';
require_once '../class/user.php';
require_once 'encryption.php';
require_once 'session.php';
require_once '../class/authentication.php';
and in the index.php page I had included
require_once 'include/include.php';
When I open the page index.php I get the following warning and fatal error. I don't understand what causes this error. When I gave the absolute path it works. But absolute path is not a good idea i believe.
Warning: require_once(../class/database.php) [function.require-once]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/otsg/include/include.php on line 9
Fatal error: require_once() [function.require]: Failed opening required '../class/database.php' (include_path='.:/Applications/MAMP/bin/php5.3/lib/php:/Applications/MAMP/htdocs/otsg/include/:') in /Applications/MAMP/htdocs/otsg/include/include.php on line 9
Use
__DIR__
to get the current path of the script and this should fix your problem.
So:
require_once(__DIR__.'/../class/user.php');
This will prevent cases where you can run a PHP script from a different folder and therefore the relatives paths will not work.
Edit: slash problem fixed
for php version 5.2.17 __DIR__ will not work it will only works with php 5.3
But for older version of php dirname(__FILE__) perfectly
For example write like this
require_once dirname(__FILE__) . '/db_config.php';
In my case it doesn't work, even with __DIR__ or getcwd() it keeps picking the wrong path, I solved by defining a costant in every file I need with the absolute base path of the project:
if(!defined('THISBASEPATH')){ define('THISBASEPATH', '/mypath/'); }
require_once THISBASEPATH.'cache/crud.php';
/*every other require_once you need*/
I have MAMP with php 5.4.10 and my folder hierarchy is basilar:
q.php
w.php
e.php
r.php
cache/a.php
cache/b.php
setting/a.php
setting/b.php
....
I just came across this same problem, where it was all working fine, up until the point I had an includes within another includes.
require_once '../script/pdocrud.php'; //This worked fine up until I had an includes within another includes, then I got this error:
Fatal error: require_once() [function.require]: Failed opening required '../script/pdocrud.php' (include_path='.:/opt/php52/lib/php')
Solution 1. (undesired hardcoding of my public html folder name, but it works):
require_once $_SERVER["DOCUMENT_ROOT"] . '/orders.simplystyles.com/script/pdocrud.php';
Solution 2. (undesired comment above about DIR only working since php 5.3, but it works):
require_once __DIR__. '/../script/pdocrud.php';
Solution 3. (I can't see any downsides, and it works perfectly in my php 5.3):
require_once dirname(__FILE__). '/../script/pdocrud.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.