Home >Backend Development >PHP Tutorial >Use PHP to write a web-based file management system_PHP tutorial
php (as the current mainstream development language) has many functions related to the file system, which can not only help you open files, but also display directory contents, move files, etc. Many people even use php(as the current mainstream development language) to write web-based file managers.
First of all, I need to remind you something about the file path: in Windows you can use slash "/" or backslash "" in the file path, while other operating systems only use "/". For compatibility reasons, the following examples use the "/" notation:
The following simple script displays a basic directory listing. Comments are in the code and explain each step:
<? /* Give the full path to the directory you want to access in the variable $dir_name*/
$dir_name = /home/me/;
/* Create a handle to open the result of the given directory*/
$dir = opendir($dir_name);
/* Start adding a piece of text to the to-be-placed list The place of element (file name) */
$file_list = <ul>;
/* Use the while statement to read all elements of the opened directory. If the file name is neither "." nor "..", then output the file name in the list*/
while ($file_name = readdir($dir)) {
if (($file_name != .) && ($file_name != ..)) {
$file_list .= <li>$file_name;
}
}
/* Final list*/
$file_list .= </ul>;
/* Close the open directory handle and end php(as the current mainstream development language)code snippet*/
closedir($dir);
?>
<!-- Start your HTML -->
<HTML>
<HEAD>
<TITLE>Directory Listing</TITLE>
</HEAD>
<BODY>
<!-- Use php(as the current mainstream development language) to print the name of the directory you read -->
<P>Files in: <? echo $dir_name; ?></p>
<!-- Use php(as the current mainstream Development language) to print the directory listing -->
<? echo $file_list; ?>
</BODY>
</HTML>
At this time it is already available A directory listing. You need to remember that to read the content in a directory or file (you will see it soon), the user's php (as the current mainstream development language) operating platform must at least have read permissions on the directory or file.
The following example shows how to copy a file:
<? /* Put the path of the file to be copied into the variable $original, and the target path to be copied into the variable $copied */
$original = /home/me/mydatabasedump;
$copied = /archive/mydatabasedumo_1010;
/* Use the function copy() to copy the source file to the destination, or end with outputting an error message* /
@copy($original, $copied) or die(Couldnt copy file.);
?>
The sample script is the first step to back up the system. When the script runs, it first copies the database to a different location for security reasons. By modifying the crontab, you can execute this file at a selected time without user intervention.
Assuming Lynx is already on the system, you can create a crontab entry to run Lynx and access files. Accessing the file will run the script and create a copy of the file. The following example will run the script at 5am and then shut down Lynx: