PHP file system functions _ PHP Tutorial

Source: Internet
Author: User
Detailed description of functions related to the PHP file system. We will explain the file path: in Windows, you can use and symbols in the path, while in other operating systems, you can only use symbols. For the sake of consistency, weFile path explanation: in Windows, you can use the/and symbol in the path, but in other operating systems, you can only use the/symbol. For the sake of consistency, we use the/symbol in a unified manner.

The following php file system-related function script example shows a directory list with comments already included in the code:

 
 
  1. /* Store the full path name of the directory to be read into a variable named $ dir_name. */
  2. $Dir_name="/Home/me /";
  3. /* Create a handle whose value is the result of opening a given directory */
  4. $Dir=Opendir($ Dir_name );
  5. /* Create a text block to place the list element (file name )*/
  6. $File_list="
      " ;
    • /* Use a while statement to read all the elements in the opened Directory. if the file name is not ".." or "..", the name in the list is displayed */
    • While ($File_name=Readdir($ Dir )){
    • If ($ file_name! = ".") & ($ File_name! = "..")){
    • $ File_list.="
    • $ File_name";
    • }
    • }
    • $ File_list.="
    ";
  7. /* Close The OpenEd directory and end the PHP module */
  8. Closedir ($ dir );
  9. ?>
  10. <HTML>
  11. <HEAD>
  12. <TITLE>Directory Listing TITLE>
  13. HEAD>
  14. <BODY>
  15. <P>Files in: Echo"$ Dir_name ";?> P>
  16. Echo"$ File_list ";?>
  17. BODY>
  18. HTML>

Now we have a directory list. Note that to read a file (which will be explained later) or directory, users on the PHP running system must have at least the permission to read the file.

The following is an example of a function related to the PHP file system for copying files:

 
 
  1. /* Assign the full path of the original file to a variable named $ original, assign the full path of the copied file to a variable named $ copied */
  2. $Original="/Home/me/mydatabasedump";
  3. $Copied="/Archive/mydatabasedumo_1010";
  4. /* Use the copy () function to copy the original file. if the copy is not completed, an error message is displayed */
  5. @ Copy ($ original, $ copied) or die ("Couldn't copy file .");
  6. ?>

The example of this php file system function is a prototype of a file backup system. When this script runs, it copies the file to a different location for saving. You can modify the daemon to execute it at the specified time of the day without user intervention.

If you have installed Lynx on the system, you can create a daemon entry to access this file. accessing this file will run this script and create a copy file, the following example runs the script at five o'clock A.M. and then closes Lynx:

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

If you are running a CGI version of PHP, you can skip the Lynx section and directly call the binary file:

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


Except: in Windows, you can use the/and symbol in the path, but in other operating systems, you can only use the/symbol. For the sake of consistency, we...

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.