PHP has many functions related to the file system. these functions can not only open files, but also display contents in directories, move files, and other functions, many people even use PHP to develop an Internet-based file resource controller. The following script
PHP has many functions related to the file system. these functions can not only open files, but also display contents in directories, move files, and other functions, many people even use PHP to develop an Internet-based file resource controller.
The following script example shows a directory list with annotations included in the code:
<? /? Store 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 the list element (file name )? /
$ File_list = '<ul> ';
/? Apply a while statement to read all the elements in the opened Directory. if the file name is not "." or "...", 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 opened 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>
Now we have a directory list. Note that to read the content of a file (which will be explained later) or directory, users on the PHP running system must have at least the permission to read the file.
The following is an example of how to copy an object:
<? /? 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 ';
/? The copy () function is used to copy the source file. if the copy is not completed, an error message is displayed? /
@ Copy ($ original, $ copied) or die ('cooldn' t copy file .');
?>
This example is a prototype of a file backup system. When this script runs, it copies the file to a different position for retained. With a slightly corrected daemon, you can perform it at the specified time of the day without user interference.