Php file system examples

Source: Internet
Author: User
PHP has many functions related to the file system. these functions can not only open files, but also display contents in directories, move files, and other functions, many people even use PHP to develop Internet-based file resource managers. PHP has many functions related to the file system. these functions can not only open files, but also display contents in directories, move files, and other functions, many people even use PHP to develop Internet-based file resource managers.

The following script example shows a directory list with annotations included in the code:

<? /? Store the full path name of the directory to be read into a variable named $ dir_name. /
$ Dir_name = "/home/me /";
/? Create a handle whose value is the result of opening a given directory? /
$ Dir = opendir ($ dir_name );
/? Create a text block to place the list element (file name )? /
$ File_list = "<ul> ";
/? Use a while statement to read all the elements in the opened Directory. if the file name is not "." or "...", the name/
While ($ file_name = readdir ($ dir )){
If ($ file_name! = ".") & ($ File_name! = "..")){
$ File_list. = "<li> $ file_name ";
}
}
$ File_list. = "</ul> ";
/? Close the opened directory and end the PHP module? /
Closedir ($ dir );
?>
<! -- Start your HTML -->
<HTML>
<HEAD>
<TITLE> Directory Listing </TITLE>
</HEAD>
<BODY>
<! -- Use PHP to print the name of the directory you read -->
<P> Files in: <? Echo "$ dir_name";?> </P>
<! -- Use PHP to print the directory listing -->
<? Echo "$ file_list";?>
</BODY>
</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 how to copy an object:

<? /? Assign the full path of the source file you want to copy to a variable named $ original, and assign the full path of the copied file to a variable named $ copied? /
$ Original = "/home/me/mydatabasedump ";
$ Copied = "/archive/mydatabasedumo_1010 ";
/? Use the copy () function to copy the source file. if the copy is not completed, an error message is displayed? /
@ Copy ($ original, $ copied) or die ("Couldn't copy file .");
?>

This example 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.

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.