Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report