7

I'm trying to setup a PHP file as a cron job, where that PHP file includes other PHP files.

The file itself is located at /var/www/vhosts/domain.com/httpdocs/app/protected/classes/cron/runner.php

The include file is at /var/www/vhosts/domain.com/httpdocs/app/protected/config.php

How do I include that config file from runner.php? I tried doing require_once('../../config.php') but it said the file didn't exist.. I presume the cron runs PHP from a different location or something.

The cron job is the following..

/usr/bin/php -q /var/www/vhosts/domain.com/httpdocs/app/protected/classes/cron/runner.php

Any thoughts?

RichW
  • 10,692
  • 6
  • 26
  • 33
  • 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:13

4 Answers4

16

Your cron should change the working directory before running PHP:

cd /var/www/vhosts/domain.com/httpdocs/app/protected/classes/cron/ && /usr/bin/php -q runner.php

Note that if the directory does not exist, PHP will not run runner.php.

Lekensteyn
  • 64,486
  • 22
  • 159
  • 192
  • Perfect, did the trick nicely. Didn't realise you could run other commands from the cron line.. – RichW Feb 19 '11 at 23:06
  • 3
    You could probably try to use an absolute path as well, rather than `../../conf` your could use `realpath(dirname(dirname(__FILE__))).'/conf`. Then your first approach should work. – chelmertz Feb 19 '11 at 23:09
  • 1
    Why call another function (realpath)? On PHP < 5.3, I would just use `dirname(dirname(__FILE__)).'/conf'`, and for PHP >= 5.3 `dirname(__DIR__).'/conf'`. But this change in the cronjob does not require a change in the PHP code itself, which could save you searching in your files for possible dependencies on the working dir.` – Lekensteyn Feb 20 '11 at 10:14
2

You should use an absolute path. The cron is probably not running the script from within the directory in which it resides.

I recommend using:

require_once( dirname(__FILE__) '../../config.php )

__FILE__ is a special constant that refers to the file you're in. dirname(...) will give you the directory, which will evaluate to the absolute path of the file you wish to include.

Michael Moussa
  • 4,207
  • 5
  • 35
  • 53
  • for PHP 5.3.0+ , `dirname(__FILE__)` can be replaced with `__DIR__` . Also , the above code missed a `.` – Raptor Sep 06 '16 at 11:29
1

You can change the working directory inside your PHP script to the location of that script: chdir(__DIR__); (or, if your PHP version is before 5.3: chdir(dirname(__FILE__));) Then you can do: require_once('../../config.php')

Ewald
  • 46
  • 3
1

Is classes or cron a symbolic link? I seem to remember that php evaluates the real path instead of the symbolic path.

Consider the following directory tree:

/target/index.php

/path/sym -> /target

if you were to execute php index.php from /path/sym then the statement require_once('../require.php'); would evaluate to require_once('/require.php'); not require_once('/path/require.php');

Dunes
  • 37,291
  • 7
  • 81
  • 97