Php command line (cli) to execute the relative path of the PHP script file. Php command line (cli) to execute the relative path of the PHP script file. the working directory of the Execution Environment (getcwd () is the relative path of the php script file executed in the php command program (p PHP command line (cli). how can this problem be solved?
Execute in the php command line. when using the PHP file, the working directory of the Execution Environment (getcwd (logging handler is the directory where the PHP command program (php.exe) is located. to use the relative path in the file, you must switch the current working directory first.
Small test program:
The code is as follows:
$ Oldpath = getcwd (); // The directory where the original working directory php.exe is located
$ Path = dirname (_ FILE __);
Chdir ($ path); // switch the working directory to the directory where the current file is located
$ Fpath = "forum/readme.txt ";
$ Fp = fopen ($ fpath, "a + B"); // If you do not switch the working directory, the file cannot be found.
Fwrite ($ fp, "oldpath:". $ oldpath. "-newpath:". getcwd ());
Fclose ($ fp );
?>
This problem also occurs for programs that require regular crotab execution. Refer to the following article:
You have written a script using the php script and need to run it regularly in crontab, but the following error occurs:
The code is as follows:
The 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 add the following code
The 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)
...............
Operation failed
Try solution 2 Add the following code
The code is as follows:
$ Cur_dir = dirname (_ FILE __);
Chdir ($ cur_dir );
Require (../class/a. php)
Require (../class/B. php)
Running successful
Summary: If you run the php script in crontab in the relative directory of require, you must enter the directory where the script is located.
How to solve the problem of executing the relative path of the PHP script file under cli is executed under the php command line. when the php file is executed, the working directory (getcwd () of the execution environment is the php command program (p...