PHP CLI mode execution file, require loading path error

Source: Internet
Author: User
Tags php cli php script

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.