0

I have a cronjob that is running off a php script which works with one exception. The task of the script is to take a file and replicate it, if there are any files of a certain type in a given folder or any of its subfolders.

If I go to the script that is what I want the cronjob to be in my browser, the script works as expected however when the cronjob runs it doesn't work says file can't be found (not the script, but the file I am trying to replicate)

Which has me wondering what type of path am I supposed to use with a cronjob?

Currently the folder structure is /public_html/cron.php /public_html/sub_folder/sub_folder/FileName.ext

testing it in the browser with file_exists() using a path like ./sub_folder/sub_folder/FileName.ext throws true

running through the cron it throws false. Which has me wondering what type of path should I be using and from which level of the server?

chris
  • 36,115
  • 52
  • 143
  • 252
  • 2
    Just use the full file system path from the server root, beginning with / - that is the easiest way. Otherwise, you have to take the include_path configuration into account, and that is often different when scripts are run as a cron via CLI, because of the latter using a different php.ini – CBroe Oct 02 '16 at 21:37
  • Duplicate of: http://stackoverflow.com/questions/1969374/relative-path-not-working-in-cron-php-script – JazzCat Oct 02 '16 at 21:37
  • 1
    `__DIR__` is really helpful in situations like this –  Oct 02 '16 at 21:39
  • This is my first time in a long time writing a cron of any kind, and none as intricate as this one, I appreciate the help, based on the duplicate mention brought up by @JazzCat I solved my problem by changing the directory within the script. So It would read from there outward so I could keep the relative path format since I am giving this to someone sooner rather than later and having them need as little configuration as possible is helpful to me. – chris Oct 02 '16 at 22:14
  • Plz add piece of code next time to know properly, I believe you might be using some $_SERVER variable in ur script, which are available on browser visit cos it calls server but not when you run php script directly using cli. – user769889 Oct 02 '16 at 22:15
  • `chdir()` is more helpful than ` __DIR__` – symcbean Oct 02 '16 at 23:01

1 Answers1

0

Instead

$path = './sub_folder/sub_folder/FileName.ext';

Use

$path =  __DIR__.'/sub_folder/sub_folder/FileName.ext';