Make your own free website on Tripod.com

Visit the Official PHP Website

Search this Site

PHP Programming

By James N Hitz

copy(), rename(), unlink(), rewind() and file()

copy()
The copy() function is used to duplicate a given file. The syntax for using copy() is:

$sucessOrFail = copy("original_src_file", "duplicate_dest_file");

rename()
rename() - as the name suggests, changes the name of one file from one thing to another. Of course the name supplied could be invalid, in a restricted location or already in use. Your script should be able to figure out if the renaming succeeded or failed. As such, the rename() function, just like the copy() function returns success or failure. The syntax:

$sucessOrFail = rename("old_file_name", "new_file_name");

unlink()
Next comes unlink(). This is a function that could have been called delete(). But there is no delete() function in PHP. Actually I think the guys at php.net should make a clone for unlink() called delete() - for the benefit of Windows enthusiasts - poor Window-holics! Hey don't take me wrong, I am not criticising... too many people have done too much of that already! It's not healthy anywayz.

The unlink() function deletes a file. It accepts the filename as an argument and like the two previous functions returns a success or failure "signal":

$sucessOrFail = unlink("file_to_erase");

rewind()
At any one given time when you open, write to, or read from a file there is a virtual file pointer that indicates the current file position. Think of it like it's the cursor in your word processor. This file pointer (cursor) can be re-positioned to the beginning of a file using the rewind() function.

rewind() accepts the $filehandle as an argument and does not return anything in return. Therefore, the syntax for using the rewind() function is:

rewind($filepointer);

file()
file() on the other hand is a useful function that creates an array containing the lines in the file. A line is considered to be terminated by a new-line character. The syntax on how to use the file() function is:

$array = file("filename");

Let's create an example that makes use of most of what we've learnt so far regarding files. We create a mailing list.

<< Functions | Reaping Time >>

JamHitz Productions