Solution to the relative path of php script file execution under PHP command line (CLI)

Source: Internet
Author: User
Tags pear php file php script relative require

When executing a. php file under the PHP command line, the working directory of the Execution Environment (GETCWD ()) is the directory of the PHP Command Program (php.exe), so if you want to use a relative path within the file, switch the current working directory first.

Small test Program:

The code is as follows:

  

$oldpath = GETCWD (); Original working directory Php.exe directory

$path = DirName (__file__);

ChDir ($path); Toggles the working directory to the directory where the current file resides

$fpath = "Forum/readme.txt";

$fp = fopen ($fpath, "a+b"); If you do not switch the working directory, you will report an error that cannot find the file

Fwrite ($FP, "OldPath:". $oldpath. " -newpath: ". GETCWD ());

Fclose ($FP);

?>

Programs that need to be executed regularly with Crotab will also have this problem. Refer to the following article:

Write a script using 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 resolving Method 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)

...............

Run failed

Try resolving Method 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: In require, if the relative directory, in the crontab run PHP script, to enter the script directory can

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.