Using PHP to write a web-based file management system

Source: Internet
Author: User

PHP has a lot of file system- related functions, not only can help you open the file, you can also display the contents of the directory, mobile files and so on. Many people even use PHP to write a web-based file manager.

First you need to remind you of something about the file path: In Windows you can use a slash "/" or "backslash" in the file path, while the other operating system uses only "/". For compatibility reasons, the following example uses the representation of "/":

The following simple script shows a basic list of directories. Comments are in the code and explain each step:

? * * Give the full path of the directory you want to access in the variable $dir _name * *

$dir _name =/home/me/;

/* Create a handle to open the result of a given directory

$dir = Opendir ($dir _name);

/* Start a piece of text to add to the place where the list element (file name) will be placed * *

$file _list = <ul>;

/* Use the while statement to read all the elements of the Open directory. If the file name is not "." and ".." The file name is printed in the list.

while ($file _name = Readdir ($dir)) {
if ($file _name!=.) && ($file _name!=.)) {
$file _list. = <li> $file _name;
}
}

/* END LIST * *

$file _list. = </ul>;

/* Close Open Directory handle and end PHP code snippet * *

Closedir ($dir);

? >

!--Start your HTML-->

<HTML>
<HEAD>
<TITLE> Directory Listing </TITLE>
</HEAD>

<BODY>
!--use PHP to print the name of this directory you read-->
<p> Files in:? echo $dir _name;? > </p>
!--use PHP to print the directory listing-->
? echo $file _list;? >
</BODY>
</HTML>


There is already a list of directories. Remember that to read directories or files (which you will see right away) the PHP platform in which the user resides must have Read permission at least for the directory or file.

The following example is how to copy a file:

? * * Put the file path to be copied into the variable $original, the copied target path into the variable $copied * *

$original =/home/me/mydatabasedump;
$copied =/archive/mydatabasedumo_1010;

/* Use function Copy () to copy the source file to the destination, or to end with an output error message.

@copy ($original, $copied) or die (couldn ' t copy file.);

? >

The


Sample script is the first step in the backup system. When the script runs, for Security , consider that it first copies the database to a different location. By modifying the crontab, you can execute the file at the selected time without user intervention.

assumes that there are already lynx on the system, and you can create crontab portals to run Lynx and access files. Accessing the file will run the script and create the copy file. The following example runs the script at 5 o'clock in the morning and then closes Lynx:

0 5 * * * [username] lynx-dump http://localhost/copyfile.php 1>/dev/null 2 >&1 

If you are running the CGI version of PHP, you can skip the Lynx section and refer to the binaries:

0 5 * * * [Username] php/path/to/copyfile.php 1>/dev/null 2>&1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.