Today, a colleague suddenly told me that a php script I wrote to do scheduled tasks always failed to execute.
The script itself is very simple. It only contains a few library files and executes a function. There should be no errors in the function. This function has been called elsewhere and there is no problem. I accessed the page locally using a browser, and the execution was successful. It seemed that there was no problem. I suspected that it was a problem with my colleague's scheduled task. I opened the command line again and used PHP to directly execute the script. The error was reported. It was an error in the require include file. It seemed that the path was wrong.
I don’t know what caused it, but judging from the error report, the path is wrong. I guess it is related to the relative path, so I changed all the relative paths to absolute paths, and then executed it again. The problem was solved. .
After searching on Baidu, I found out that when using PHP to execute a script, the location where the script is executed is the path where the PHP command is currently executed, so the relative path is incorrect. There are two ways to solve the problem:
1. Use absolute paths instead. This is more troublesome. All included files include the file paths included in directly included files. If one of them is wrong, an error may occur, so it can only be used when executing the file is relatively simple.
2. Use the chdir function to change the current execution directory to the directory where the file is located, chdir(dirname(__FILE__));