Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using require_once for up directory not working

I am using require_once like this

require_once('../mycode.php') 

I am developing a wordpress plugin. My plugin folder is yves-slider where I have a file called yves-slider.php and a folder called admin. Inside admin folder I have a file called admin.php. I want to require file yves-slider.php in my admin.php which is located up one level directory. When I try to use

require_once('../yves-slider.php') 

it gives me the following error

Warning: require_once(../yves-slider.php): failed to open stream: No such file or directory in C:\xampp\htdocs\wordpress\wp-content\plugins\yves-slider\yves-slider-admin\yves-slider-admin.php on line 4

Fatal error: require_once(): Failed opening required '../yves-slider.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\wordpress\wp-content\plugins\yves-slider\yves-slider-admin\yves-slider-admin.php on line 4

Am I doing wrong? I am using XAMPP 3.1, I guess that's the best way to do it.

like image 443
Yves Gonzaga Avatar asked Jan 06 '13 16:01

Yves Gonzaga


People also ask

What is the main difference between require () and require_once ()?

The basic difference between require and require_once is require_once will check whether the file is already included or not if it is already included then it won't include the file whereas the require function will include the file irrespective of whether file is already included or not.

What is the difference between include_once and require_once in PHP?

include_once will throw a warning, but will not stop PHP from executing the rest of the script. require_once will throw an error and will stop PHP from executing the rest of the script.

HOW include file in parent directory in PHP?

If the referenced object is a folder, the function will return the parent folder of that folder. For the current folder of the current file, use $current = dirname(__FILE__); . For a parent folder of the current folder, simply use $parent = dirname(__DIR__); .

How does require_once work in PHP?

The require_once keyword is used to embed PHP code from another file. If the file is not found, a fatal error is thrown and the program stops. If the file was already included previously, this statement will not include it again.


1 Answers

You want to make that relative to the current path the file is in:

require_once __DIR__ . '/../yves-slider.php'; 

What probably is happening is that the current path PHP looks in is not the path you think it is. If you are curious about what it is (the current path) you could do echo getcwd();.

like image 134
PeeHaa Avatar answered Sep 17 '22 13:09

PeeHaa