Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the relative path of executing the PHP script file under the php command line

Shulou Source: shulou.com Published: 2022-06-03 18:27:04 10月05日 Update

This article mainly explains the "php command line under the implementation of the PHP script file relative path how to solve", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in-depth, together to study and learn "php command line under the implementation of PHP script file relative path how to solve" it!

When executing a .php file under the php command line, the working directory of the execution environment (getcwd ()) is the directory where the php command program (php.exe) is located, so if you want to use a relative path within the file, you need to change the current working directory first.

Mini-test program:

The copy code is as follows:

Programs that need to be executed regularly with crotab will also have this problem. Please refer to the following article:

A script was written using the php script, which needs to be run regularly in crontab, but the following error occurs

The code is as follows:

The copy code is as follows:

/ var/www/html/bt/e/BtSys:.:/usr/share/pear:/usr/share/phpPHP Warning: require (.. / class/connect.php): failed to open stream: No such file or directory in / var/www/html/bt/e/BtSys/torrents-scrape.php on line 17

PHP Fatal error: require (): Failed opening required'.. / class/connect.php' (include_path='/var/www/html/bt/e/BtSys:.:/usr/share/pear:/usr/share/php') in / var/www/html/bt/e/BtSys/torrents-scrape.php on line 17

Try solution 1 by adding the following code

The copy code is as follows:

/ / setting include path

$cur_dir=getcwd ()

$cur_dir=$basedir = dirname (_ _ FILE__)

$path = ini_get ('include_path')

Ini_set ("include_path", "$cur_dir:$path")

$path = ini_get ('include_path')

/ / echo $path

Require. / class/a.php)

Require. / class/b.php)

.

Failed to run

Try solution 2 by adding the following code

The copy code is as follows:

The copy code is as follows:

$cur_dir = dirname (_ _ FILE__)

Chdir ($cur_dir)

Require. / class/a.php)

Require. / class/b.php)

Run successfully

Summary: in require, if it is a relative directory, run the php script in crontab, you can only enter the directory where the script is located.

Thank you for your reading, the above is the "php command line under the implementation of PHP script file relative path to ask how to solve" the content, after the study of this article, I believe you on the php command line under the implementation of PHP script file relative path to ask how to solve this problem has a more profound experience, the specific use of cases also need to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Code directory file script command path work location run program switch learning content method article error problem attempt original success Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MariaDB Xiaomi Apple Shulou Technology