13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Using Program Execution Functions<br />

447<br />

Creating, Deleting, <strong>and</strong> Moving Files<br />

You can use the file system functions to create, move, <strong>and</strong> delete files.<br />

First, <strong>and</strong> most simply, you can create a file, or change the time it was last modified,<br />

using the touch() function.This function works similarly to the Unix comm<strong>and</strong> touch.<br />

The function has the following prototype:<br />

bool touch (string file, [int time [, int atime]])<br />

If the file already exists, its modification time will be changed either to the current time<br />

or the time given in the second parameter if it is specified. If you want to specify this<br />

time, you should give it in timestamp format. If the file doesn’t exist, it will be created.<br />

The access time of the file will also change: by default to the current system time or<br />

alternatively to the timestamp you specify in the optional atime parameter.<br />

You can delete files using the unlink() function. (Note that this function is not<br />

called delete—there is no delete.) You use it like this:<br />

unlink($filename);<br />

You can copy <strong>and</strong> move files with the copy() <strong>and</strong> rename() functions, as follows:<br />

copy($source_path, $destination_path);<br />

rename($oldfile, $newfile);<br />

You might have noticed that we used copy() in Listing 19.2.<br />

The rename() function does double duty as a function to move files from place to place<br />

because <strong>PHP</strong> doesn’t have a move function.Whether you can move files from file system to<br />

file system <strong>and</strong> whether files are overwritten when rename() is used are operating system<br />

dependent, so check the effects on your server.Also, be careful about the path you use to<br />

the filename. If relative, this will be relative to the location of the script, not the original file.<br />

Using Program Execution Functions<br />

Let’s move away from the file system functions now <strong>and</strong> look at the functions available<br />

for running comm<strong>and</strong>s on the server.<br />

These functions are useful when you want to provide a web-based front end to an<br />

existing comm<strong>and</strong>-line–based system. For example, you previously used these comm<strong>and</strong>s<br />

to set up a front end for the mailing list manager ezmlm.You will use them again when<br />

you come to the case studies later in this book.<br />

You can use four main techniques to execute a comm<strong>and</strong> on the web server.They are<br />

all relatively similar, but there are some minor differences:<br />

n<br />

exec()—The exec() function has the following prototype:<br />

string exec (string comm<strong>and</strong> [, array &result [, int &return_value]])<br />

You pass in the comm<strong>and</strong> that you would like executed, as in this example:<br />

exec(“ls -la”);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!