© Robert Aley 2016

Rob Aley, PHP CLI, 10.1007/978-1-4842-2238-6_5

5. PHP CLI Scripts and Your System

Rob Aley

(1)Oxford, UK

PHP CLI scripts typically interact with your system in a number of areas. While most system interactions are identical to those you’re used to with web scripts, there are a few areas you may not have used much, so I’ll cover a few gotchas that might trip you up.

Starting External Processes from PHP, or “Shelling Out”

A common feature of CLI scripts is calling and interacting with other scripts and processes. Using other scripts and programs as “building blocks” within your script is a common pattern of CLI programming. As you saw earlier, you can often combine your PHP script with others in general-purpose shell scripts, but often it is useful for the PHP script itself to start other software when it needs to do so. This is often referred to as shelling out.

A number functions are available in PHP to achieve this, each doing it in a slightly different way and with different benefits .

  • exec(): Executes a program and sends the text output to the user.

  • passthru(): Executes a program and sends the binary output to the user.

  • system(): Executes a program and gathers the output for use by PHP.

  • shell_exec(): Executes a command via a shell and gathers its output for use by PHP.

  • Backtick operator (for example, `command`): Identical to shell_exec() earlier.

  • pcntl_exe(): Executes a program in the current process space; that is, stops the current PHP script and replaces it with the specified program.

  • popen(): Executes a program and opens a file pointer (identical to the pointers returned by fopen(), for example) to read or write to the process via STDOUT or STDIN. Can only read or write, not both.

  • proc_open(): Like popen(), but with more control. Allows both reading and writing at the same time. Not as simple to use as popen().

Which method you choose depends on what you intend to do with the newly opened process and how (or indeed if) you want to talk to it. If you’re not sure from the earlier descriptions which function is appropriate for your use, the “Further Reading” list gives some pointers, information, and examples of implementations that should give you some direction.

Further Reading

When calling external scripts , remember that using untrusted user input in command names or options is a recipe for bad security! The escapeshellarg() function can protect you from some inadvertent mistakes but won’t stop you from executing “bad” functions or files.

Further Reading

File Status and Realpath Caches

On the Web, speed is king. PHP operates two information caches to speed up access to the file system. The first is the file status cache, which caches information about a given file (such as whether it exists, whether it is readable, its size and type, and so on). The second is the realpath cache, which caches the actual, real path for a given file or directory (expanding symlinks, relative paths, . and .. paths, include_paths and so on). Information is added to the cache automatically by PHP each time it encounters a new file and is then used by any number of functions the next time they attempt to look at that same file. With a web page that’s gone in the blink of an eye where little may have happened on the file system, this is often a good trade-off for increased performance.

However, the chances that the details of a file or path may change while your script runs obviously increase with the length of time that your script takes to execute. Therefore, PHP gives you a couple of options for working with these two caches.

The following example shows the file status cache in action and how to use clearstatcache() to clear it:

<?php

# Create a file and add some text to it

$filename = 'test.txt';

$handle = fopen($filename, 'w+');

fwrite($handle, 'test');

# The following should print 4

echo stat($filename)["size"]." ";

# Now write some data to the file, increasing the file size.

fwrite($handle, 'more test');

# Intuitively, the following command should print 13 as the file is now
# bigger than before. However it still prints 4, because the filesize
# value for this file is now cached.


echo stat($filename)["size"]." ";

# If we clear the cache ....

clearstatcache();

# then the next line should print 13 as expected

echo stat($filename)["size"]." ";

fclose($handle);

The realpath cache operates in a similar way and can be cleared by calling clearstatcache(true), in other words, by calling it with true as the first parameter. You can also clear the cache for just one particular file by calling clearstatcache(true, 'myfile.txt'), where the second parameter is the file name (the first must be set to true; that is, you must also clear the realpath cache).

Of course, clearing these caches may not be necessary in your application, and doing so has performance implications. Consider each file access on a case-by-case basis.

APC and Other Code Caches

You may be aware of the APC caching system and other code caching systems that act to speed up the startup time of your script. APC, and most other systems, will not work with CLI scripts (or at least not add any benefit) because they work on a shared process model found with web servers such as Apache. PHP CLI scripts (typically) terminate their own process when they complete, so they will not work.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset