A case of file resource management in PHP programming

Source: Internet
Author: User
Tags copy file system php programming resource

PHP has many file system-related functions that not only open files, but also display content, move files, and other features in the directory, and many people even use PHP to develop internet-based file Explorer.

The following script sample can display a list of directories that are already included in the code:

? / the full path name of the directory to be read into a variable named. /

= "/home/me/";

/ creates a handle whose value is the result of opening a given directory /

= Opendir ();

/ creates a block of text to place the list element (file name) /

= "

    ";

/ uses a while statement to read all the elements in a directory that is already open, if the file name is not "." or "..", the names in the list are displayed /

while (= Readdir ()) {

if ((!= ".") &&&& (!= "...")) {

. = "

  • ";

  • }

    }

    . = "";

    / closes the Open Directory and ends the PHP module /

    Closedir ();

    ? >

    !--Start your HTML-->

    Directory Listing

    !--use PHP to print the name of this directory you read-->

    Files in:

    !--use PHP to print the directory listing-->

    ? echo "";

    Well, we've got a list of directories. It is important to note that to read a file (which we will explain later) or the contents of the directory, the user on the system on which PHP is running must have at least the right to read the file.

    Here is an example of how to copy a file:

    ? / assign the full path of the source file you want to copy to a variable named, and assign the full path of the copied file to a variable named /

    = "/home/me/mydatabasedump";

    = "/archive/mydatabasedumo_1010";

    / uses the copy () function to copy the source file and displays an error message if the copy is not completed /

    @copy (,) or Die ("couldn ' t copy file.");

    ? >

    This example is a prototype of a file backup system. When the script runs, it copies the files to a different location for saving. By modifying the daemon a little, you can execute it at the moment you specify in the day without the intervention of the user.



    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.