15 October 2006

Code Library - Shell_exec()

Hi Friends,

I would like to share the knowledge some thing I gathered in PHP

Shell_exec():

Use : we can execute shell scripts from a php file

Similar Concept : Like Function Calling Statement in C & C++

Example:

shell_exec("php -q MonthlyTime.php ". $StartDate." ".$EndDate." ".$location_id);

shell_exec("php -q MonthlyTime.php ". 2006-09-27 ." ".2006-09-31." ". 15 );

Here
php –q -> command to run a php file
MonthlyTime.php -> php file that I would like to run

date('Y-m-d',strtotime($StartDate))
date('Y-m-d',strtotime($EndDate))
$location_id

These are parameters that I have passed to the file “MonthlyTime.php”

Note: please maintain a blank space between parameters

How do I receive the parameters ( values ) in the file “MonthlyTime.php”:

$StartDate = $argv[1];
$enddate = $argv[2];
$LocationID = $argv[3];

Here we can receive those values from an array $argv you can pass ‘n’ number of arguments and also receive.

Practical Application:

Defaulter.php

If ( $ResultFlag==1 )
{
shell_exec("php -q MonthlyTime.php ".$StartDate." ".$EndDate." ".$location_id);

}

Else
{
shell_exec("php -q MonthlyHour.php ".$StartDate." ".$EndDate." ".$location_id);

}
?>

Here we are checking a condition for $ResultFlag and we are executing a php file at the run time by using shell_exec(); When our file reads the command shell_exec() then the program control will switch over to that file like “MonthlyTime.php” then the control will return to the next line in the parent file like Defaulter.php”

I hope now you will get an idea about shell_exec( ); in php


Here we are checking a condition for $ResultFlag and we are executing a php file at the run time by using shell_exec();

When our file reads the command shell_exec() then the program control will switch over to that file like “MonthlyTime.php” then the control will return to the next line in the parent file like “Defaulter.php”

I hope now you will get an idea about shell_exec( ); in php



1 comment:

A. Mohamed Rafiq said...

hai sister, thanks for ur information..... i know little bit in php...... i hope that ur information will be useful to me in future.... anyway thanks a lot for ur information...... keep it up.............