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 /* Iterate through directory contents. */ @closedir($dh); $dir = "/usr/local/apache2/htdocs/book/chapter10/test/"; 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.
blog comments powered by Disqus |
|
|
|
|
|
|
|