There are many file system-related functions in PHP. These functions not only open files, but also display content in directories, move files and some other functions. Many people even use PHP to develop an Internet-based file explorer.
The following script example can display a directory listing, and comments are already included in the code:
<? /Save the full path name of the directory to be read into a variable named $dir_name. /
$dir_name = "/home/me/";
/ Create a handle whose value is the result of opening a given directory /
$dir = opendir($dir_name);
/ Create a text block to place list elements (file name)/
$file_list = "<ul>";
/ Use a while statement to read all elements in the directory that has been opened, and if the name of the file is not "." or "..", then the name in the list is displayed/
while ($file_name = readdir($dir)) {
if (($file_name != ".") &&&& ($file_name != "..")) {
$file_list .= "<li>$file_name";
}
}
$file_list .= "</ul>";
/Close the open directory and end the PHP module/
closedir($dir);
?>
<!-- Start your HTML -->
<HTML>
<HEAD>
<TITLE>Directory Listing</TITLE>
</HEAD>
<BODY>
<!-- Use PHP to print the name of the directory you read -->
<P>Files in: <? echo "$dir_name"; ?></p>
<!-- Use PHP to print the directory listing -->
<? echo "$file_list"; ?>
</BODY>
</HTML>
OK, we've got a directory listing. It should be noted that to read the contents of a file (we will explain it later) or directory, users on the system running PHP must have at least permission to read the file.
Here is an example of how to copy a file:
<? / Assign the full path of the source file you want to copy to a variable named $original, and assign the full path of the copied file to a variable named $copied.
$original = "/home/me/mydatabasedump";
$copied = "/archive/mydatabasedumo_1010";
/ Use the copy() function to copy the source file. If the copy is not completed, an error message will be displayed.
@copy($original, $copied) or die("Couldn't copy file.");
?>
This example is a prototype of a file backup system. When this script is running, it copies the file to a different location for saving. A little bit of modification to the daemon can be executed at the moment of the day without user intervention.
The following script example can display a directory listing, and comments are already included in the code:
<? /Save the full path name of the directory to be read into a variable named $dir_name. /
$dir_name = "/home/me/";
/ Create a handle whose value is the result of opening a given directory /
$dir = opendir($dir_name);
/ Create a text block to place list elements (file name)/
$file_list = "<ul>";
/ Use a while statement to read all elements in the directory that has been opened, and if the name of the file is not "." or "..", then the name in the list is displayed/
while ($file_name = readdir($dir)) {
if (($file_name != ".") &&&& ($file_name != "..")) {
$file_list .= "<li>$file_name";
}
}
$file_list .= "</ul>";
/Close the open directory and end the PHP module/
closedir($dir);
?>
<!-- Start your HTML -->
<HTML>
<HEAD>
<TITLE>Directory Listing</TITLE>
</HEAD>
<BODY>
<!-- Use PHP to print the name of the directory you read -->
<P>Files in: <? echo "$dir_name"; ?></p>
<!-- Use PHP to print the directory listing -->
<? echo "$file_list"; ?>
</BODY>
</HTML>
OK, we've got a directory listing. It should be noted that to read the contents of a file (we will explain it later) or directory, users on the system running PHP must have at least permission to read the file.
Here is an example of how to copy a file:
<? / Assign the full path of the source file you want to copy to a variable named $original, and assign the full path of the copied file to a variable named $copied.
$original = "/home/me/mydatabasedump";
$copied = "/archive/mydatabasedumo_1010";
/ Use the copy() function to copy the source file. If the copy is not completed, an error message will be displayed.
@copy($original, $copied) or die("Couldn't copy file.");
?>
This example is a prototype of a file backup system. When this script is running, it copies the file to a different location for saving. A little bit of modification to the daemon can be executed at the moment of the day without user intervention.