Today, my colleague suddenly told me that the execution of a scheduled PHP script that I wrote was always unsuccessful.
The script itself is simple, only contains a few library files and executes a function, the function should have no errors, this function has been called elsewhere, no problem. I visited the page locally with a browser, the execution was successful, it seems that there is no problem, I am a bit skeptical of a colleague's scheduled tasks. I open the command line again, using PHP to execute the script directly, this return is wrong, is require contains file errors, it seems that the path is not correct.
Do not understand what causes, but from the error point of view is the path is not correct, I guess is related to the relative path, so the relative path of all to change to absolute path , and then execute, problem solving.
After the search on Baidu, know that the script is executed in PHP, the location of the execution of the script is the current execution of the PHP command path, so the relative path is wrong. There are two ways to solve a problem:
First, the use of absolute path, this is more troublesome, all included files include files contained in the file path, there is a wrong can be wrong, so only when the execution of the file is relatively simple to use.
Second, use the ChDir function, change the current execution directory, the directory to the directory where the file, ChDir (dirname (__file__));
PHP CLI mode execution file, require loading path error