I have a problem with phpUnit.
Whenever I try to open a file using require (or any other similar function) which is not in the same directory or subdirectory as the test file itself, phpUnit throws this error:
Warning: include_once Failed opening "C:\....." for inclusion (include path = '.;C:\xampp\php\PEAR')
Can anybody tell me what I am doing wrong? Thank you
try using realpath and dirname to get a full path
# ../myfolder/file.php
$path = realpath(dirname(dirname(__FILE__)))."/myfolder/file.php"
# ./file.php
$path = realpath(dirname(__FILE__))."/file.php"
# ../file.php
$path = realpath(dirname(dirname(__FILE__)))."/file.php"
# ../../file.php
$path = realpath(dirname(dirname(dirname(__FILE__))))."/file.php"
Related
I'm trying to manually install PhpPowerpoint (not using composer)
I've WAMP installation having document_root D:\wamp\www\
i've copied the PhpPowerpoint folder under the following path D:\wamp\www\php\PhpOffice\
using the manual installation code (the code is in a file D:\wamp\www\php\agile\expMSPowerpoint.php):
require_once '../PhpOffice/PhpPowerpoint/Autoloader.php';
PhpOffice\PhpPowerpoint\Autoloader::register();
$objPHPPowerPoint = new PhpPowerpoint(); <<<< Error in this line
i get the error:
Fatal error: Class 'PhpPowerpoint' not found in D:\wamp\www\php\agile\expMSPowerpoint.php on line 435
Any help to get it installed would be much appreciated
I suggest using the PHP realpath http://php.net/manual/en/function.realpath.php
From the manual page:
realpath() expands all symbolic links and resolves references to
'/./', '/../' and extra '/' characters in the input path and returns
the canonicalized absolute pathname.
I have an error that happens only on my host server, on my local environment not:
Fatal error: require_once() [function.require]: Failed opening required '\Audero\Loader\AutoLoader.php' (include_path='.:/usr/share/pear:/usr/share/php:/home/httpd/vhosts/webox-it.com/ofrom.webox-it.com/modules/Concordancier....\lib') in /home/httpd/vhosts/webox-it.com/ofrom.webox-it.com/modules/Concordancier/data_manager.php on line 1670
Here is my code:
set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/../../lib/');
require_once 'Audero\Loader\AutoLoader.php';
spl_autoload_register('Audero\Loader\AutoLoader::autoload');
// Extract the chunk and save it on the hard disk
try {
$extractor = new \Audero\WavExtractor\AuderoWavExtractor($inputFile);
$extractor->saveChunk($start, $end, $outputFile);
...
Files structure:
/lib/Audero/Loader/Autoloader.php
Calling file emplacement:
/modules/Concordancier/data_manager.php
In my local environment there's no problem, php can load the autoloader, but in the prod server it does not.
The source files structure is exactly the same as the local one, and the relative path looks correct. The php version in prod is 5.3.27 so it should work ?
Any idea ?
I have PHPUnit set up in my PHPStorm project. I've referenced the PHPUnit phar file and have a PHP executable linked to my PHPStorm run configuration. My directory structure looks like:
/lib/classes/Class.php
/lib/vendor/phpunit.phar
/lib/test/ClassTest.php
In my ClassTest.php file, I reference the other two files with:
require_once (__DIR__ . "../vendor/phpunit.phar");
require_once (__DIR__ . "../classes/Class.php");
I get the following error when I run my test:
Fatal error: require_once(): Failed opening required
'C:\Users\me\PhpstormProjects\myproject\lib\tests../vendor/phpunit.phar'
It seems like the PHP parser isn't correctly parsing the up one directory ../ command.
Why is this happening?
First of all: this line is not needed (at all) as PHPUnit will already be loaded at that time.
require_once (__DIR__ . "../vendor/phpunit.phar");
Secondly: __DIR__ constant in PHP does not contain trailing slash. When used in require/include statements (and other places when building full file path) you have to add it yourself.
In your particular case it has to be (note / before ..):
require_once (__DIR__ . "/../classes/Class.php");
I got the following error when a file is included.
Warning: require(home2/myusername/public_html/inc/config.php)
[function.require]: failed to open stream: No such file or directory in
/home2/myusername/public_html/inc/bootstrap.php on line 32
Fatal error: require() [function.require]: Failed opening required
'home2/myusername/public_html/inc/config.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in
/home2/myusername/public_html/inc/bootstrap.php on line 32
It is working fine in my localhost running on Windows PC. But I got that error when I uploaded the files on my shared hosting server which is CentOS.
home2/myusername/public_html/index.php includes /inc/bootstrap.php which is then trying to include /inc/config.php. The file config.php does exist on the server. home2/myusername/public_html/ is returned by the function getcwd().
The below is the first few lines of codes in bootstrap.php which issues the error.
define('APP_DIR', 'app');
if( !defined('APP_ROOT') ){
$APP_ROOT = trim(getcwd(), '/').'/';
if(isset($_GET['bootstrap'])) $APP_ROOT .= APP_DIR . '/';
define('APP_ROOT', $APP_ROOT);
}
if( !defined('ROOT') ){
$ROOT = str_replace(APP_DIR, '', trim(APP_ROOT, '/'));
if( strrpos($ROOT, '/') != strlen($ROOT)-1 ) $ROOT .= '/';
define('ROOT', $ROOT);
}
# path to inc/ folder
define('INC', ROOT.'inc/');
# System configuration variables
require INC . 'config.php';
I have also a URL Rewrite in /public_html/.htaccess.
RewriteRule ^index.php$ index.php?bootstrap [L]
So, when I browse example.com/index.php, it rewrites to example.com/index.php?bootstrap. It is the situation I got the error.
Here is my directory structure:
/public_html/
|__ inc
| |__ bootstrap.php
| |__ config.php
|__ .htaccess
|__ index.php <--- I'm browsing this
I think the problem would be related to the absolute path file include. The relative path file include require 'inc/bootstrap.php in index.php is okay.
If you file is located in:
/home2/myusername/public_html/index.php
and you bootstrap.php file to include is located in:
/home2/myusername/public_html/inc/bootstrap.php
your include line correctly is:
include_once $_SERVER['DOCUMENT_ROOT']."/inc/bootstrap.php";
$_SERVER['DOCUMENT_ROOT'] is equal to /home2/myusername/public_html
Most likely your paths are not correctly configured. It's an easy solution most of the time. Can you please post the code of config.php and bootstrap.php where you include the files.
It's most likely that you have to change the following.
/inc/config.php to config.php.
Since it's in the same folder.
EDIT:
You are missing a '/'
Failed opening required 'home2/myusername/public_html/inc/config.php'
Should be this
'/home2/myusername/public_html/inc/config.php'
I am working on ubuntu 12.04. I installed HTTP_Client by sudo pear install HTTP_Client. But when I am using require_once 'HTTP/Client.php';. It's showing:
Warning: require_once(HTTP/Client.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream: No such file or directory in /var/www/mai.php on line 3
How I remove this error?
How paths are resolved when using include or require is controlled by the include_path php.ini setting, which is typically set to:
include_path=".:/usr/share/php"
Whereby /usr/share/php points to where PEAR is installed.
It can be set using set_include_path() during runtime as well:
set_include_path(".:/usr/share/php");
Should be run before you include anything else.
'HTTP/Client.php' is a relative path. The error message means that the file doesn't exist at that location. You'll need to modify the path, or move the file.
In this case your PHP file is in "/var/www/" and there is no HTTP directory there.
Edit: I would recommend modifying the path to be the full path rather than the relative path:
require_once 'HTTP/Client.php';
Change to:
require_once '/usr/share/php/HTTP/Client.php';