Skip to Content
Learning PHP, MySQL, JavaScript, and CSS, 2nd Edition
book

Learning PHP, MySQL, JavaScript, and CSS, 2nd Edition

by Robin Nixon
August 2012
Beginner
583 pages
16h 1m
English
O'Reilly Media, Inc.
Content preview from Learning PHP, MySQL, JavaScript, and CSS, 2nd Edition

System Calls

Sometimes PHP will not have the function you need to perform a certain action, but the operating system it is running on may. In such cases, you can use the exec system call to do the job.

For example, to quickly view the contents of the current directory, you can use a program such as the one in Example 7-17. If you are on a Windows system, it will run as-is using the Windows dir command. On Linux, Unix, or Mac OS X, comment out or remove the first line and uncomment the second to use the ls system command. You may wish to type in this program, save it as exec.php, and call it up in your browser.

Example 7-17. Executing a system command
<?php // exec.php
$cmd = "dir";   // Windows
// $cmd = "ls"; // Linux, Unix & Mac

exec(escapeshellcmd($cmd), $output, $status);

if ($status) echo "Exec command failed";
else
{
    echo "<pre>";
    foreach($output as $line) echo "$line\n";
}
?>

Depending on the system you are using, the result of running this program will look something like this (from a Windows dir command):

Volume in drive C is HP
Volume Serial Number is E67F-EE11

Directory of C:\web

20/01/2011  10:34
            .
    20/01/2011  10:34
                ..
        19/01/2011  16:26               236 maketest.php
        20/01/2011  10:47               198 exec.php
        20/01/2011  08:04            13,741 smiley.jpg
        19/01/2011  18:01                54 test.php
        19/01/2011  16:59                35 testfile.txt
        20/01/2011  09:35               886 upload.php
                    6 File(s)        15,150 bytes
                    2 Dir(s)  382,907,748,352 bytes free

exec takes three arguments:

  1. The command itself (in the previous case, $cmd)

  2. An array in which the system will put ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning PHP, MySQL & JavaScript, 4th Edition

Learning PHP, MySQL & JavaScript, 4th Edition

Robin Nixon

Publisher Resources

ISBN: 9781449337452Errata Page