Home arrow PHP arrow Page 3 - Executing Shell Commands with PHP

Executing Shell Commands - PHP

In this sixth part of an eight-part series on working with the file and operating systems of a computer with PHP, you'll learn how to use PHP to write to a file and how to execute shell commands. 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. Executing Shell Commands with PHP
  2. Reading Directory Contents
  3. Executing Shell Commands
By: Apress Publishing
Rating: starstarstarstarstar / 0
December 07, 2011

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The ability to interact with the underlying operating system is a crucial feature of any programming language. This section introduces PHP's capabilities in this regard.

PHP's Built-in System Commands

Although you could conceivably execute any system-level command using a function like exec() or system(), some of these functions are so commonplace that the developers thought it a good idea to incorporate them directly into the language. Several such functions are introduced in this section.

rmdir()

int rmdir (string dirname)

The rmdir() function removes the directory specified by dirname, returning TRUE on success and FALSE otherwise. As with many of PHP's file system functions, permissions must be properly set in order for rmdir() to successfully remove the directory. Because PHP scripts typically execute under the guise of the server daemon process owner, rmdir() will fail unless that user has write permissions to the directory. Also, the directory must be empty.

To remove a nonempty directory, you can either use a function capable of executing a system-level command, like system() or exec(), or write a recursive function that will remove all file contents before attempting to remove the directory. Note that in either case, the executing user (server daemon process owner) requires write access to the parent of the target directory. Here is an example of the latter approach:

<?php
function delete_directory($dir)
{
if ($dh = @opendir($dir))
{

/* Iterate through directory contents. */
while (($file = readdir ($dh)) != false)
{
if (($file == ".") || ($file == "..")) continue;
if (is_dir($dir . '/' . $file))
delete_directory($dir . '/' . $file);
else
unlink($dir . '/' . $file);
} #endWHILE

@closedir($dh);
rmdir($dir);
} #endIF
} #end delete_directory()

$dir = "/usr/local/apache2/htdocs/book/chapter10/test/";
delete_directory($dir);
?>

rename()

boolean rename (string oldname, string newname)

The rename() function renames a file specified by oldname to the new name newname, returning TRUE on success and FALSE otherwise. Because PHP scripts typically execute under the guise of the server daemon process owner, rename() will fail unless that user has write permissions to that file.

touch()

int touch (string filename [, int time [, int atime]])

The touch() function sets the file filename's last-modified and last-accessed times, returning TRUE on success or FALSE on error. If time is not provided, the present time (as specified by the server) is used. If the optional atime parameter is provided, the access time will be set to this value; otherwise, like the modification time, it will be set to either time or the present server time.

Note that if filename does not exist, it will be created, assuming that the script's owner possesses adequate permissions.

Please check back for the next part of the series.



 
 
>>> 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: