When executing files in php cli mode, the require loading path is wrong.

WBOY
Release: 2016-07-29 09:11:31
Original
1195 people have browsed it

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. There seemed to be no problem. I suspected it was a problem with my colleague's scheduled task. I opened the command line again and used php to execute the script directly. The error was reported. It was arequireincluded file error. It seemed that the path was wrong.

 I don’t know what caused it, but judging from the error report, it seems that the path is wrong. I guess it is related to the relative path, so I changed all the relative paths toabsolute paths, and then executed it again, and 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:

First, use absolute paths. 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 only when executing file comparison Available when simple.

  2. Use the chdir function to change the current execution directory to the directory where the file is located, chdir(dirname(__FILE__));

The above introduces the executable file in php cli mode. The require loading path is wrong, including the require content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!