1

I have 2 servers that are on completely separate OS' and configurations. One properly accepts require_once '../file.php'; and the other doesn't, because it's relative to where the cron is loading I guess.

How can I fix the 2nd location so that it's relative path is from the relative path of the file. I need this to work in 3 different environments (local, beta, and live) so I don't want to write a bunch of code on this page, I'd prefer a configuration adjustment if possible.

Ben
  • 60,438
  • 111
  • 314
  • 488
  • possible duplicate of [Relative path not working in cron PHP script](http://stackoverflow.com/questions/1969374/relative-path-not-working-in-cron-php-script) – Alastair Irvine Mar 19 '14 at 06:11

2 Answers2

3

Chane the working directory to the running file path. Just use

chdir(dirname(__FILE__));

include_once '../your_file_name.php'; //we can use relative path after changing directory

in the running file. Then you no need alter every relative path to absolute one in all pages. This works fine for me.

Withfriendship Hiox
  • 1,287
  • 2
  • 8
  • 5
2

__FILE__ will specify the full file path of the current script.

realpath() translates paths with relative components into absolute paths.

This should work:

require_once (realpath(dirname(__FILE__)."/../file.php"));
Pekka
  • 442,112
  • 142
  • 972
  • 1,088