Php command line (cli) to execute the relative path of the PHP script file solution

Source: Internet
Author: User
This article mainly introduces how to execute the relative path of the php script file under the PHP command line (cli), especially when running the PHP script in crontab, you can solve the problem by referring to the methods described in this article. For more information, see 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:


<? Php
$ 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:

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.

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.