The next series of scripts will help you perform very basic file system tasks, such as copying, renaming, and deleting files. Remember that you can perform file system functions only if the proper permissions are in place for the PHP user.
The copy() function is very simple: it needs to know the original filename and a new filename, and that's all there is to it.
Open a new file in your text editor and start a PHP block:
<?
Create a variable to hold the full path name to the original file (use your own path):
$orig_filename = "/Documents and Settings/Owner/Desktop/misc/textfile.txt";
Create a variable to hold the full path name to the new file (use your own path):
$new_filename = "/Documents and Settings/Owner/Desktop/misc/textfile.bak";
Create a variable to hold the true/false result of the function. Suppress warnings by using the @ in front of the function, and use die() to print a message if the function fails:
$success = @copy($orig_filename, $new_filename) or die("Couldn't copy file.");
Start an if…else statement to print the proper message based on the outcome of the function:
if ($success) {
The message string, if successful, should print a confirmation of the copy:
$msg = "Copied $orig_filename to $new_filename";
Continue the statement for a failure, and then close the PHP block:
| Note |
Using the else statement in this case is actually unnecessary, but it's good practice for providing a default result. If the copy() function fails, the die() function will exit the script and print the error before even getting to the if…else part of the script. |
Add this HTML:
<HTML> <HEAD> <TITLE>Copy a File</TITLE> </HEAD> <BODY>
Print the message:
<? echo "$msg"; ?>
Add some more HTML so that the document is valid:
</BODY> </HTML>
Save the file with the name copyfile.php, and place this file in the document root of your web server.
Open your web browser and type http://127.0.0.1/copyfile.php.
See if your error handling works by changing the value of $new_filename to something that doesn't exist:
$new_filename = "/bozo/textfile.bak";
Access the script via your web browser, and you should see the appropriate message.
Next, let's move on to renaming files. The script is remarkably similar!
Like the copy() function, the rename() function just needs to know the original filename and a new filename. In this case, you're just renaming the original, not copying it.
Open a new file in your text editor and start a PHP block:
<?
Create a variable to hold the full path name to the original file (use your own path):
$orig_filename = "/Documents and Settings/Owner/Desktop/misc/textfile.bak";
Create a variable to hold the full path name to the new file (use your own path):
Create a variable to hold the true/false result of the function. Suppress warnings by using the @ in front of the function, and use die() to print a message if the function fails:
$success = @rename($orig_filename, $new_filename)
or die("Couldn't rename file.");
Start an if…else statement to print the proper message based on the outcome of the function:
if ($success) {
The message string, if successful, should print a confirmation of the renaming function:
$msg = "Renamed $orig_filename to $new_filename";
Continue the statement for a failure, and then close your PHP block:
} else {
$msg = "Could not rename file.";
}
?>
| Note |
As in the previous script, the else statement in this case is unnecessary, but it's good practice for providing a default result. |
Add this HTML:
<HTML> <HEAD> <TITLE>Rename a File</TITLE> </HEAD> <BODY>
Print the message:
Add some more HTML so that the document is valid:
</BODY> </HTML>
Save the file with the name renamefile.php, and place this file in the document root of your web server.
Open your web browser and type http://127.0.0.1/renamefile.php.
See if your error handling works by changing the value of $new_filename to something that doesn't exist:
$new_filename = "/bozo/textfile.bak";
There's one more housekeeping function in the next section: deleting files.
Be very careful when using the unlink() function, because once you've deleted a file, it's gone for good.
Open a new file in your text editor and start a PHP block:
<?
Create a variable to hold the full path name to the file you want to delete (use your own path):
Create a variable to hold the true/false result of the function. Suppress warnings by using the @ in front of the function, and use die() to print a message if the function fails:
$success = @unlink($filename) or die("Couldn't delete file.");
Start an if…else statement to print the proper message based on the outcome of the function:
if ($success) {
The message string, if successful, should print a confirmation of the deletion:
$msg = "Deleted $filename";
Continue the statement for a failure, and then close your PHP block:
} else {
$msg = "Could not delete file.";
}
?>
| Note |
As in the previous scripts, using the else statement in this case is unnecessary, but it's good practice for providing a default result. |
Add this HTML:
<HTML> <HEAD> <TITLE>Delete a File</TITLE> </HEAD> <BODY>
Print the message:
<? echo "$msg"; ?>
Add some more HTML so that the document is valid:
Save the file with the name deletefile.php, and place this file in the document root of your web server.
Open your web browser and type http://127.0.0.1/deletefile.php.
See if your error handling works by changing the value of $filename to something that doesn't exist:
$filename = "/bozo/textfile.old";
Access the script via your Web browser, and you should see this:
In the next chapter, you create a two-step process (front-end form and back-end script) to initiate file uploads from a web browser to your file system.