Home >Backend Development >PHP Tutorial >Solution to require loading path error reported in php command line (cli) mode_php tips
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. There seemed to be no problem. I suspected it was a problem with my colleague's scheduled task. I opened the command line again and used php to directly execute the script. This 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 was wrong. I guessed it was related to the relative path, so I changed all the relative paths to absolute paths, then executed it, and 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__));
Finally, let’s briefly introduce PHP’s cli command line mode
First, enter PHP's CLI (Command line interface) mode
cyguin-> $ ./php --ini(显示配置文件信息)
will display:
configuration file(php.ini) path: c:/Windows Loaded configuration file: c:/wamp/bin/php/php5.3.0/php.ini scan for additional .ini files in: (none) additional .ini files parsed: (none)
$./php --help (will display PHP specific information)
$./php --rf 8a11bc632ea32a57b3e3693c7987c420 (display information about 8a11bc632ea32a57b3e3693c7987c420 function)
$./php --fc 8a11bc632ea32a57b3e3693c7987c420 (display information of 8a11bc632ea32a57b3e3693c7987c420 class)
$./php --re 8a11bc632ea32a57b3e3693c7987c420 (display 8a11bc632ea32a57b3e3693c7987c420 extended information)
$./php --ri 8a11bc632ea32a57b3e3693c7987c420 (display 8a11bc632ea32a57b3e3693c7987c420 extended configuration)
$./php -l 28897b20adb25fbae118a3f80f538dec : Check syntax
For example: $./php -l c:/wamp/www/1.php
Running results: No Syntax errors detected in xxx
$./php -v : Display version information and running mode cgi/cli
Running result:
PHP 5.3.0 (cli) (built: Jun 29 2009 21:25:23)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies
$./php -f 28897b20adb25fbae118a3f80f538dec:Parse and execute the file
For example: $./php -f c:/wamp/www/1.php
$./php -h : Display help file
$./php -r ffbe95d20f3893062224282accb13e8f: Run PHP code without using the bb9bd6d87db7f8730c53cb084e6b4d2d tag
For example: $./php -r 'print("hello!");' or $./php -r 'echo "hello!";'
Running result: hello!
$./php -a : Enter interactive mode
Under linux it will appear:
php>
Under windows:
Required input:
<?php echo "hello world!"; ?>
Then ctl c to get the output.
In PHP5.3.0, it cannot be used directly because the result cannot be obtained directly.
<?php echo "hello world!"; ?>
Enter ctl z first, then ctl c, and you will get the result.
$./php -c : Specify the location to search for php.ini, or specify a specific ini file.
$./php -n : Do not use php.ini
$./php -d foo [=bar] : Define the value of foo entry in the INI file to be bar
$./php -m : Display all modules, php modules and zend modules
$./php -e : Generate extended information for debugger/profiler
$./php -B 6f51180bb1dda8e53081ff7b2e6e8602 : Code for processing standard input check execution. New in PHP5.
Such as:
$./php -B 'echo "hello world";'
Output result: hello world
$./php -R ffbe95d20f3893062224282accb13e8f : PHP code executed for each line. New in PHP5.
$./php -F 28897b20adb25fbae118a3f80f538dec :
$./php -H 9fdf4c0bbbe72496ae7cf95851354ef1 : Code executed after processing the input
$./php -s : Display HTML syntax highlighted code
$./php -w : Get the code with spaces and comments removed
Such as:
$./php -w c:/www/1.php
$./php -z 28897b20adb25fbae118a3f80f538dec : Load Zend extension file
$pwd: Display the current path
--enable-cli, --enable-cgi