Solution to the problem of executing the relative path of the php script file under the PHP command line (CLI)
When executing a. php file at the PHP command line, the working directory for the Execution Environment (GETCWD ()) is the directory where the PHP command program (Php.exe) is located, so if you want to use relative paths within a file, you must first switch the current working directory.
Small test Program:
The code is as follows:
$oldpath = GETCWD (); directory where the original working directory Php.exe
$path = DirName (__file__);
ChDir ($path); Switch 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 to the working directory here will not find the file error
Fwrite ($FP, "OldPath:". $oldpath. " -newpath: ". GETCWD ());
Fclose ($FP);
?>
This is also the case with programs that need to be executed with crotab timing. You can refer to this article below:
A script was written with PHP script that needs to be run periodically 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 L INE 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 workaround 1 by adding 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)
...............
Failed to run
Try workaround 2 by adding the following code
The code is as follows:
$cur _dir = dirname (__file__);
ChDir ($cur _dir);
Require (.. /class/a.php)
Require (.. /class/b.php)
Run successfully
Summary: When require, if it is relative directory, run PHP script in crontab, to go to the directory where the script can be
http://www.bkjia.com/PHPjc/1005314.html www.bkjia.com true http://www.bkjia.com/PHPjc/1005314.html techarticle Workaround for the PHP command Line (CLI) to execute the relative path of the php script file when executing the. php file at the PHP command line, the working directory for the Execution Environment (GETCWD ()) is the PHP command program (P ...