Use PHP to maintain the file system

Source: Internet
Author: User
PHP has many file system-related functions that not only help you open files, but also display directory content and move files. Many even use PHP to write a Web-based file manager. First, you need to remind you about the file path: in Windows, you can use the slash "/" or the backslash SyntaxHighlighter. all () in the file path ();

PHP has many file system-related functions that not only help you open files, but also display directory content and move files. Many even use PHP to write a Web-based file manager.

First, you need to remind you about the file path: in Windows, you can use the slash "/" or backslash "" in the file path, while other operating systems only use the slash "/". For compatibility considerations, the following instances use the "/" representation:

The following simple script shows a basic directory list. Note the following in the code and explain each step:

$ Dir_name = "/home/me /";

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

$ Dir = opendir ($ dir_name );

/* Start a text section and add it to the location where the list element (file name) will be placed */

$ File_list ="

    ";

    /* Use the while statement to read all the elements in the opened directory. If the file name is neither "." nor "..", output the file name in the list */

    While ($ file_name = readdir ($ dir )){

    If ($ file_name! = ".") & ($ File_name! = "..")){

    $ File_list. ="

  • $ File_name ";

    }

    }

    /* End List */

    $ File_list. ="

";

/* Close The OpenEd directory handle and end the PHP code segment */


Closedir ($ dir );

?>

Directory Listing

Files in:

Congratulations, there is a directory list. Remember that to read contents in a directory or file (you will see it soon), the user's PHP runtime platform must have at least the read permission on the directory or file.

The following example shows how to copy a file:

$ Original = "/home/me/mydatabasedump ";

$ Copied = "/archive/mydatabasedumo_1010 ";

/* Use the function copy () to copy the source file to the destination, or end with an output 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 copies the database to different locations for security reasons. By modifying crontab, you can execute this file at the selected time without user intervention.


If Lynx already exists on the system, you can create a crontab entry to run Lynx and access the file. Accessing the file will run the script and create a copy file. The following example runs the script at am 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 binary file:

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.