Executing files in php cli mode, require loading path error, _PHP tutorial

WBOY
Release: 2016-07-12 09:04:21
Original
1076 people have browsed it

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

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__));

 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1073809.htmlTechArticleWhen executing the file in php cli mode, the require loading path is wrong. Today, a colleague suddenly told me that I wrote a The execution of the php script of the scheduled task is always unsuccessful. The script itself is simple...
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template