Home > Backend Development > PHP Tutorial > Maintain file system using PHP_PHP tutorial

Maintain file system using PHP_PHP tutorial

WBOY
Release: 2016-07-21 16:09:31
Original
809 people have browsed it

PHP has many functions related to the file system, which not only help you open files, but also display directory contents, move files, etc. Many people have even written web-based file managers using PHP.

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 simple script below displays a basic directory listing. Comments are 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 adding a piece of text to Where the list elements (file names) will be placed */

$file_list = "

    ";

    /* 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 .= "

  • $file_name";

    }

    }

    /* Terminate list*/

    $file_list .= "

";

/* Close open directory handle and terminate PHP code Section*/


closedir($dir);

?>

Directory Listing

< BODY>

Files in:

Congratulations, you now have a directory listing. You need to remember that to read the contents of a directory or file (you will see it soon), the PHP running platform where the user is located must have at least read permission on the directory or file.

The following example is 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 output error message*/

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

?>

The sample script is the first step in backing up your 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:

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 part and refer to the binaries:

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


-------------------------------- -----------
The author of this article: Julie Meloni is the technical director of i2i Interactive and a strong promoter of the Linux and open source communities. She has written numerous books on PHP and other technologies, and is a long-time contributing expert to CNET Builder.com.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/314491.htmlTechArticlePHP has many functions related to the file system, which not only help you open files, but also display directory contents and move Documents etc. Many people even use PHP to write web-based file management...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template