Home  >  Article  >  Backend Development  >  Solution to relative path problem in php command line

Solution to relative path problem in php command line

angryTom
angryTomforward
2019-10-14 16:06:092758browse

Generally, PHP is run as a CGI and Apache module, and PHP is rarely run under the command line (CLI).

When using include, require, etc. in a PHP file under the (CLI) command line, you must first switch the current working directory before using a relative path, otherwise an error will be reported and the file cannot be found.

Let’s take a look at the perfect solution to the relative path problem under the PHP command line (CLI).

$cur_dir = dirname(__FILE__); //获取当前文件的目录
chdir($cur_dir); //把当前的目录改变为指定的目录。
require(../class/a.php);//引入相对路径文件

Another small detail, when using PHP to write a newline in a file, sometimes using \r\n does not take effect. Please check whether \r\n uses single quotes. Double quotes must be used here.

In this way, we can use PHP to do some scheduled processing when the number of visits is small. In fact, in conjunction with crontab scheduled tasks under Linux, PHP can help us do a lot of things. For example, today's statistics, database backup, log clearing and some daily maintenance, etc.

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of Solution to relative path problem in php command line. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:www.leixuesong.cn. If there is any infringement, please contact admin@php.cn delete