PHP CLI: Paths

From The Uniform Server Wiki
Revision as of 09:49, 15 August 2009 by Ric (talk | contribs) (New page: {{Uc nav PHP CLI}} '''''Introduction and basics''''' On the previous page I mentioned the simple “Hello Word” example masks a few issues such as paths and interactions. This page tak...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

 

MPG UniCenter

UniServer 5.0-Nano
PHP CLI.

Introduction and basics

On the previous page I mentioned the simple “Hello Word” example masks a few issues such as paths and interactions.

This page takes a detailed look at these and how to resolve them.

Interaction problem

Note: Skip this section and jump to paths UniSever 5.0-Nano uses PHP 5.3.0, which does not suffer from this issue. That said if you wish to see this issue first hand run the example on any of the Mona series.

A quick test to highlight the interaction problem copy the two test files Run.bat and test_1.php from folder cli_test to folder UniServer\udrive\usr\local\php

Run.bat test_1.php
TITLE CLI TEST BAT
COLOR B0
@echo off
cls
echo.
php.exe test_1.php
echo.
pause
<?php
echo "\nHello World\n";
exit(0); // Script ran OK
?>

Run the batch file (double click on Run.bat) a number of error messages are returned from main program php5ts.dll e.g:

PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/PHP/extensions\php_curl.dll' - The specified modul
e could not be found.
 in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/PHP/extensions\php_gd2.dll' - The specified module
 could not be found.
 in Unknown on line 0

The main program requires set-up instructions, by default it looks for these in a file named php-cli.ini and php.ini starting in the current folder.

If the files do no exist, searches along all Windows paths for them. Should it fail to find any uses built-in defaults for configuration, hence no error seen because defaults work.

However if a php.ini is picked up configuration errors will be displayed.

Top

Problem

The main program picks-up UniServer's php.ini and uses that.

It contains instructions specific for a server these use paths which cannot be resolved, hence all the errors messages.

Top

Solution

You can create a new php-cli.ini file specifically for running CLI scripts (see later) or instruct the main program not to use a set-up file and to use internal default settings (these are tailored specifically for CLI operation). To achieve this the interface program php.exe takes a parameter "-n" and passes it to the main program, edit Run.bat, change the line to:

php.exe -n test_1.php

Run the batch file, this time you will see only the "Hello World" message.

Note 1: Majority of CLI scripts tend not to require a php-cli.ini file hence use the -n switch.

Note 2: PHP 5.3.0 defaults to -n if it cannot find a php-cli.ini hence is not an issue, however for backwards computability always add -n

Top

Paths

For good house keeping; paths are important. They allow you to locate your PHP scripts in convenient folders well away from the main PHP program, CLI interface program and configuration file.

When a program is installed such as PHP it is put on the system path. To run a script from the command prompt you would type something like php.exe \test\test1.php. Whats important you only need to specify the path to your script; path to php.exe is automatically picked up because its on the system path.


However UniServer does not install anything to the registry or system paths what this means is you must specify a path to php.exe as well as a path to your script.

The following example demonstrates this.

Run.bat test_1.php
TITLE CLI TEST BAT
COLOR B0
@echo off
cls
echo.
php.exe test_1.php
echo.
pause
<?php
echo "\nHello World\n";
exit(0); // Script ran OK
?>
  • Either copy or create file test_1.php with content shown above and save to folder UniServer\usr
  • Either copy or create file Run.bat with content shown above and save to folder UniServer

Running the batch file Run.bat fails to run your test script this is because of incorrect paths to php.exe and test_1.bat

Edit Run.bat as follows:

TITLE CLI TEST BAT
COLOR B0
@echo off
cls
echo.
usr\local\php\php.exe -n usr\test_1.php
echo.
pause

Note: Paths are defined relative to the batch file, you can use absolute paths for example H:\z_test\UniServer\usr\test_1.php however moving the scripts to another location requires changing all paths to match that new location.

Run the batch file (double click on Run.bat) will display the "hello world" message.

Tip: When it comes to typing long paths I am lazy and its error prone hence I right click on the destination file, select properties, cut and past.

Top

Current working directory

This next example is intended to highlight paths are referenced to the caller.

Edit test_1.php to have the following content:

<?php
echo "\nHello World\n";
echo system("dir");
exit(0); // Script ran OK
?>

The instruction echo system("dir") displays the content of the current working folder.

Run the batch file (double click on UniServer\Run.bat) it runs script test_1.php displays "Hello World" and the content of folder UniServer.

It does not display the content of folder UniServer\usr as is sometimes expected.

Top

Current working directory path

PHP provides two powerful instructions that allow you to capture the current working directory (folder) and directory where the current script resides. These allow you to easily move around the folder tree.

* getcwd() – function to capture current working directory (folder)
* dirname(__FILE__) - function to capture directory of the current script
* chdir() – function allowing you to change the current working directory

Edit test file UniServer\usr\test_1.php change content to:

<?php
echo getcwd()." -- Current WD directory\n";   // Just echo current working directory
$a = getcwd();                                // Save into variable
echo "$a -- Current WD saved in variable \$a\n";

echo dirname(__FILE__)." -- Directory where this script resides\n";
$b = dirname(__FILE__);                        // Save this to a variable
echo "$b -- Directory where this script resides saved in variable \$b\n";

chdir(dirname(__FILE__));                      // Change wd to this files location
echo getcwd()." -- New CW directory set using chdir(dirname(__FILE__))\n";

echo  "\n========= ".getcwd()." ========\n";
echo system("dir /b");                         // Display its content

$c = getcwd()."/../main/key_cert_gen";         // Move up one-level and down into folder key_cert_gen
chdir ($c);                                    // make that the current working directory
echo  "\n\n========= ".getcwd()." ========\n"; // display its path and 
echo system("dir /b");                         // display its content

chdir ($a);                                    // Change back to original working directory
echo  "\n\n========= ".getcwd()." ========\n"; // display its path and 
echo system("dir /b");                         // display its content
echo ("\n\n");  
exit(0); // Script ran OK
?>

Run the batch file (double click on UniServer\Run.bat) output as per script.

Note: Saving the initial working directory in a variable (e.g $a) allows you to return to that directory at any time using chdir($a)

Top

Current working PHP 5.3

As an alternative to dirname(__FILE__) PHP 5.3 introduced __DIR__

Edit test file UniServer\usr\test_1.php change content to:

<?php
echo getcwd()." -- Current WD directory\n";   // Just echo current working directory
$a = getcwd();                                // Save into variable
echo "$a -- Current WD saved in variable \$a\n";

echo __DIR__." -- Directory where this script resides\n";
$b = __DIR__;                                 // Save this to a variable
echo "$b -- Directory where this script resides saved in variable \$b\n";

chdir(__DIR__);                               // Change wd to this files location
echo getcwd()." -- New CW directory set using chdir(__DIR__)\n";

echo  "\n========= ".getcwd()." ========\n";
echo system("dir /b");                         // Display its content

$c = getcwd()."/../unicon/key_cert_gen";       // Move up one-level and down into folder key_cert_gen
chdir ($c);                                    // make that the current working directory
echo  "\n\n========= ".getcwd()." ========\n"; // display its path and 
echo system("dir /b");                         // display its content

chdir ($a);                                    // Change back to original working directory
echo  "\n\n========= ".getcwd()." ========\n"; // display its path and 
echo system("dir /b");                         // display its content
echo ("\n\n");  
exit(0); // Script ran OK
?>

Run the batch file (double click on UniServer\Run.bat) output as per script.

Note: There is no change in functionality it just reduces amount of typing however think about backwards compatibility before using it.

Top

Clean-up

Before moving onto the next examples move file UniServer\usr\test_1.bat to folder UniServer. While in this edit Run.bat and update the path to test_1.bat as shown:

TITLE CLI TEST BAT
COLOR B0
@echo off
cls
echo.
usr\local\php\php.exe -n test_1.php
echo.
pause

Run the batch file (double click on UniServer\Run.bat) check it runs our test script. Ignore one warning its expected anyways proves the script ran.

Top

Summary

Apart from creating and using a CLI ini file that concludes the basics for running CLI scripts.

You may never require a CLI ini file however it is easy to set-up as covered on the next page as an extra bonus I cover installing a module specific to CLI. This module provides statistics for all currently running process.

Top


MPG (Ric)