Home arrow PHP arrow PHP File and Operating System Program Execution Functions

PHP File and Operating System Program Execution Functions

In this conclusion to an eight-part article series on working with a computer's file and operating system with PHP, you'll learn about the functions used in combination with a PHP script to execute system-level programs. This article is excerpted from chapter 10 of the book Beginning PHP and PostgreSQL 8: From Novice to Professional, written by W. Jason Gilmore and Robert H. Treat (Apress; ISBN: 1590595475).

TABLE OF CONTENTS:
  1. PHP File and Operating System Program Execution Functions
  2. More PHP Program Execution Functions
By: Apress Publishing
Rating: starstarstarstarstar / 0
January 03, 2012

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

PHP's Program Execution Functions

This section introduces several functions (in addition to the backticks execution operator) used to execute system-level programs via a PHP script. Although at first glance they all appear to be operationally identical, each offers its own syntactical nuances.

exec()

string exec (string command [, array output [, int return_var]])

The exec() function is best-suited for executing an operating system-level application (designated by command) intended to continue executing in the server background. Although the last line of output will be returned, chances are that you'd like to have all of the output returned for review; you can do this by including the optional parameter output, which will be populated with each line of output upon completion of the command specified by exec(). In addition, you can discover the executed command's return status by including the optional parameter return_var.

Although we could take the easy way out and demonstrate how exec() can be used to execute an ls command (dir for the Windows folks), returning the directory listing, it's more informative to offer a somewhat more practical example: how to call a Perl script from PHP. Consider the following Perl script (languages.pl):

#! /usr/bin/perl
my @languages = qw[perl php python java c]; foreach $language (@languages) {
print $language."<br />";
}

The Perl script is quite simple; no third-party modules are required, so you could test this example with little time investment. If you're running Linux, chances are very good that you could run this example immediately, because Perl is installed on every respectable distribution. If you're running Windows, check out ActiveStates (http://www.activestate.com/) ActivePerl distribution.

Like languages.pl, the PHP script shown here isn't exactly rocket science; it simply calls the Perl script, specifying that the outcome be placed into an array named $results. The contents of $results are then output to the browser.

<?php
$outcome = exec("languages.pl", $results);
foreach ($results as $result) echo $result;
?>

The results are as follows:

--------------------------------------------perl
php
python
java
c
--------------------------------------------

system()

string system (string command [, int return_var])

The system() function is useful when you want to output the executed command's results. Rather than return output via an optional parameter, as is the case with exec(), the output is returned directly to the caller. However, if you would like to review the execution status of the called program, you need to designate a variable using the optional parameter return_var.

For example, suppose you'd like to list all files located within a specific directory:

$mymp3s = system("ls -1 /home/jason/mp3s/");

Or, revising the previous PHP script to again call the languages.pl using system():

<?php
$outcome = exec("languages.pl", $results);
echo $outcome
?>



 
 
>>> More PHP Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: