php how to do include right way - php

Hello i'm running WAMP (32 Bit) server
i exec C:\wamp\www\1\index.php using browser with this address http://localhost:8080/1/
index.php has the follow code
require_once 'libraries/database/database.php';
database.php has the follow code
require_once '../misc/traits/singleton.php';
singleton.php is located in this location
C:\wamp\www\1\libraries\misc\traits\singleton.php
database.php is located in this location
C:\wamp\www\1\libraries\database\database.php
and there is error(s):
( ! ) Warning: require_once(../misc/traits/singleton.php): failed to open stream: No such file or directory in C:\wamp\www\1\libraries\database\database.php on line 3
( ! ) Fatal error: require_once(): Failed opening required '../misc/traits/singleton.php' (include_path='.;C:\php\pear') in C:\wamp\www\1\libraries\database\database.php on line 3
Thanks

Paths stem from the php file that was originally executed, unless you change the path using chdir.
So use 'libraries/misc/traits/singleton.php'
If you want to do relative includes, use __DIR__, which is the directory of the CURRENT file.
e.g.
__DIR__ . '/../misc/traits/singleton.php'

Related

PHP Warning: include(): Failed opening

I have this error on my site after I moved it from a shared hosting to a VPS (both with cPanel).
PHP Warning: include(): Failed opening '../../../transporturi/a.php'
for inclusion
(include_path='.:/opt/cpanel/ea-php70/root/usr/share/pear') in [...]
on line 36.
In the other file on line 36 I have included a PHP file, I checked the path and it's there. I also tried to switch PHP version from the WHM from 5.5 to 5.6 to 7.0, but no luck.
The path is relative to the file that received the request, so you can use the PHP DIR magic constant to know what is your current directory:
echo(__DIR__);
and then form the absolute path relative to your file as below.
include(__DIR__ . '../../../transporturi/a.php');
Now it depends on your directory structure and the location of both the included file and the one where its included.

How to have relative path (../) in a pipe script?

I have [root]/includes/helpdesk/pipe.php with this code:
#!/opt/cpanel/ea-php55/root/usr/bin/php
<?php
require_once("pipeprocess.php");
In the same location in pipeprocess.php in first line I include another file still in the same location: helpdesk.php
then in [root]/includes/helpdesk/helpdesk.php in the first line I have:
require_once ("../../config.php");
config.php is in [root], this is why I have twice ../../, helpdesk.php works fine with direct access, but if run pipe.php via pipe command setup in cPanel I get a bounce error that config. php is not found:
PHP Warning: require_once(../../config.php): failed to open stream:
No such file or directory in
/home/[user]/public_html/[root]/includes/helpdesk/helpdesk.php on line
21
Warning: require_once(../../config.php): failed to open stream: No
such file or directory in
/home/[user]/public_html/[root]/includes/helpdesk/helpdesk.php on line
21 PHP Fatal error: require_once(): Failed opening required
'../../config.php' (include_path='.:/usr/lib/php:/usr/local/lib/php')
in /home/[user]/public_html/[root]/includes/helpdesk/helpdesk.php on
line 21
Fatal error: require_once(): Failed opening required
'../../config.php' (include_path='.:/usr/lib/php:/usr/local/lib/php')
in /home/[user]/public_html/[root]/includes/helpdesk/helpdesk.php on
line 21
Why I am getting this pipe error as direct access to helpdesk.php works fine? does pipe not accept ../ or ../../ as parth of the path? or is there an EasyApache4 issue? I did several tests and noticed this is because of ../ as part of including path. Does pipe/EasyApache4 have problem with this?
What if you used __DIR__, based on where your currently running script file's folder?
As of PHP 5.3.0, you could use something like this:
require_once (__DIR__ . "/../../config.php");

PHP fatal error opening required file

I'm having problems debugging this php error. '/app/bootstrap.php' exists so not sure why I'm getting this error? Can anyone help? Thanks
[24-Mar-2017 10:42:50 Europe/London] PHP Warning: require_once(/app/bootstrap.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/example.com/index.php on line 2
[24-Mar-2017 10:42:50 Europe/London] PHP Fatal error: require_once(): Failed opening required '/app/bootstrap.php' (include_path='.:/Applications/MAMP/bin/php/php5.6.30/lib/php') in /Applications/MAMP/htdocs/example.com/index.php on line 2
The path /app/bootstrap.php looks like a root path ( and the root isn't your website root, but your Mac root directory).
Change require path to __DIR__ . '/app/bootstrap.php', it means to require a sccript that in /app/bootstrap.php relative to CURRENT directory.

restFul API : failed to open stream: No such file or directory

I'm working on a restful API in php.
All is working on localhost, but when I try to put it on my server, I have these errors :
Warning: include_once(/path/to/api/simpletodo_api/Models/TodoItem.php) [function.include-once]: failed to open stream: No such file or directory in /homez.338/login/www/path/to/api/simpletodo_api/index.php on line 6
Warning: include_once() [function.include]: Failed opening '/path/to/api/simpletodo_api/Models/TodoItem.php' for inclusion (include_path='.:/usr/local/lib/php') in /homez.338/login/www/path/to/api/simpletodo_api/index.php on line 6
Here my include :
include_once 'Models/TodoItem.php';
index.php and the "Models" folder are in the same folder (in local and on my server).
I'm pretty sure it is a path error, but I can't find the right one.
Have you an idea of the path ?
If your server is Linux-based and your localhost is on Windows, you may be running into file naming errors. File paths in Linux are case-sensitive. Just to be sure prepend the __DIR__ constant to your PHP path like so: include_once __DIR__ . 'Models/TodoItem.php';

Correct path to file in LiteSpeed web server on Linux

I have the following PHP script placed in the root directory of my web hosting server, and it works fine.
<?php
include(dirname(__FILE__).'/config/config.inc.php');
require_once(dirname(__FILE__).'/init.php');
When I moved the script to a new folder under my web root, i.e. public_html/myfolder, the script gives the following error:
Warning: include(/home/name/public_html/folder/config/config.inc.php) [function.include]: failed to open stream: No such file or directory in /home/name/public_html/folder/test.php on line 2
Warning: include() [function.include]: Failed opening '/home/name/public_html/folder/config/config.inc.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/name/public_html/folder/test.php on line 2
Warning: require_once(/home/name/public_html/folder/init.php) [function.require-once]: failed to open stream: No such file or directory in /home/name/public_html/folder/test.php on line 3
Fatal error: require_once() [function.require]: Failed opening required '/home/name/public_html/folder/init.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/name/public_html/folder/test.php on line 3
Should I change the path as follows:
<?php
include(dirname(__FILE__).'/../config/config.inc.php');
require_once(dirname(__FILE__).'/../init.php');
Am I correct?
Uhm, you say you only moved your script (the one that contains the include lines) to the "public_html/folder". That way, dirname(__FILE__) contains the full path, including the "folder" directory. So, now, you need to go up one step in the directory tree, to be able to reach the "config" directory; an explanation follows:
Before you had:
/home/name/public_html/config/config.inc.php
/home/name/public_html/init.php
So your file
/home/name/public_html/test.php
Managed to access the files using the path from dirname(__FILE). But now, the test.php has been movedto the directory "folder":
/home/name/public_html/folder/test.php
Thus, dirname(__FILE__) should return the path comprehensive of the "folder" directory. But the files you need to include are in teh "old" directory, so youneed to add that ../ to the path of the include, to be abletoreach those files :)

Categories