38

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.

cristi _b
  • 1,783
  • 2
  • 28
  • 43
Yves Gonzaga
  • 1,038
  • 1
  • 16
  • 40

1 Answers1

69

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();.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • 1
    What the hell I just miss one "/" in your example '/../yves-slider.php' :D thanks so much.. you answer will be checked in 6 minutes. Why the hell do I need one "/"? instead of directly '../yves-slider.php' – Yves Gonzaga Jan 06 '13 at 16:45
  • but i wonder, why this kind of "relative" paths work: require_once "directory/class.php" – emfi Oct 08 '13 at 14:46
  • 1
    @emfi those are relative to the current working dir and should be avoived for flexibility in your code. – PeeHaa Oct 08 '13 at 15:02
  • well, i don't use this method, despite i don't get why there should be more flexibility? – emfi Oct 09 '13 at 05:33
  • @emfi for example when running file through the CLI the current working dir is not changed. So that means that when doing a simple `require_once "directory/class.php"` might not try to load the file in the directory you think it is. – PeeHaa Oct 09 '13 at 08:41
  • 2
    I have started a troubleshooting checklist for this frequent error here : stackoverflow.com/a/36577021/2873507 – Vic Seedoubleyew Apr 12 '16 at 15:15