0

I have a php script that works perfectly fine when run by the web server but when I run the script from the command line (or as a cron job) then it fails to find the file. The code that errors out is:

include('fpdf181/fpdf.php');

I tried including DIR but that does not work either

include(__DIR__ .'fpdf181/fpdf.php');

Can someone tell me how to include files for a script that will run from the command line

coderatlarge
  • 577
  • 3
  • 8
  • 23
  • It's running from another directory. Check this https://stackoverflow.com/questions/17616158/running-cronjob-from-a-specific-directory . You could debug it using `getcwd()` method and saving the result in a file, this way you can know what's goin on. – jeprubio May 18 '19 at 08:10
  • 1
    As `__DIR__` does not include a trailing `/` - you may need `__DIR__ .'/fpdf181/...`. I would also recommend using `require_once` instead of `include` - https://stackoverflow.com/questions/2418473/difference-between-require-include-require-once-and-include-once – Nigel Ren May 18 '19 at 08:31
  • From the manual: "Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing. The include construct will emit a warning if it cannot find a file; this is different behavior from require, which will emit a fatal error. " However a simple test on my setup, seems to favour the calling cwd first. In short, without an explicit path, this is somewhat difficult to determine! – Progrock May 18 '19 at 09:31
  • Which server do you have ? – Ronak Chauhan May 18 '19 at 10:25

3 Answers3

0

can you see this (PHP Cron Job: Including file not working?)

as the link, you use "require" and not include

Code: require('./fpdf181/fpdf.php')

happy codeing.

007fred
  • 155
  • 2
  • 8
0

You can use the full path to the file (independent of current directory / env).

include('/path/to/lib/fpdf181/fpdf.php');
0

I was able to execute my script by doing a cwd on the directory and going to the folder where the script was placed

coderatlarge
  • 577
  • 3
  • 8
  • 23