Using PHP to write a Web-based file management System _php Tutorial

Source: Internet
Author: User
PHP (now the mainstream development language) has a lot of file system-related functions, not only to help you open the file, you can also display directory content, move files and so on. Many people even write Web-based file managers using PHP, which is now the mainstream development language.

The first thing you need to do is remind yourself of something about the file path: In Windows you can use the slash "/" or the backslash "\" in the file path, while the other operating systems use only "/". For compatibility reasons, the following instances use the "/" representation:

The following simple script shows a basic list of directories. The comment is in the code and explains each step:

? /* Give the directory full path 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 text to add to the place where the list element (filename) will be placed */

$file _list =

    ;

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

while ($file _name = Readdir ($dir)) {
if ($file _name! =) && ($file _name! =)) {
$file _list. =

  • $file _name;
    }
    }

  • /* End list */

    $file _list. = ;

    /* Close Open Directory handle and end PHP (as current mainstream development language) code snippet */

    Closedir ($dir);

    ? >

    !--Start your HTML-->



    Directory Listing


    !--use PHP (as the current mainstream development language) to print the name of the directory you read-->

    Files in:? echo $dir _name;? >


    !--use PHP (as the current mainstream development language) to print the directory listing-->
    ? echo $file _list;

    A list of directories is now available. It is important to remember that to read a directory or file (as you will see in a moment), the user's PHP (which is now the mainstream development language) must have read permissions at least to the directory or file.

    Here's an example of how to copy a file:

    ? /* Place the file path you want to copy into the variable $original, the target path of the copy into the variable $copied */

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

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

    @copy ($original, $copied) or die (couldnt copy file);

    ? >

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

    Assuming Lynx is already on the system, you can create a crontab portal to run Lynx and access the files. Accessing the file runs the script and creates the copied file. The following example runs the script at 5 o'clock in the morning and then turns off Lynx:

    http://www.bkjia.com/PHPjc/508705.html www.bkjia.com true http://www.bkjia.com/PHPjc/508705.html techarticle PHP (now the mainstream development language) has a lot of file system-related functions, not only to help you open the file, you can also display directory content, move files and so on. Many people even make ...

  • 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.