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
$./php --fc
$./php --re
$./php --ri
$./php -l
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
For example: $./php -f c:/wamp/www/1.php
$./php -h : Display help file
$./php -r : Run PHP code without using the 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
Such as:
$./php -B 'echo "hello world";'
Output result: hello world
$./php -R
: PHP code executed for each line. New in PHP5.
$./php -F
$./php -H
$./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
$pwd: Display the current path
--enable-cli, --enable-cgi