PHP directory function Implementation Create, read directory tutorial examples _php tips

Source: Internet
Author: User
Tags create directory mkdir php tutorial php website readable strcmp
Today, we mainly introduce the application of file directory function in the development of PHP website. In the development of PHP website, we often need to read directory file information or create a directory to store the necessary files, and when the directory file size exceeds the specified size, we need to delete the directory files, such as manual deletion of the directory that is time-consuming and laborious, we can completely through the directory operation function PHP to implement the directory file management.
This article takes the example tutorial form to explain how uses the php file directory function, the example main function: First, uses the PHP directory function to create many directories, two, creates the text file in the directory and writes the related information in the file, three, recursively realizes reads (traverses) the Directory (folder) Information and lists all subdirectories and files under the directory in the form of a list.
This example refers to file read and write operations, the recommended first check the PHP file reading and writing tutorial.
This instance directory structure: The PHP execution file is at the same level as the LEAPSOULCN directory, and the subdirectory created is in the LEAPSOULCN directory.
Step one: Create related directories using PHP directory functions
Copy Code code as follows:

mkdir ("LEAPSOULCN", 0777);
mkdir ("Leapsoulcn/leapsoul", 0777);
mkdir ("leapsoulcn/php", 0777);
mkdir ("Leapsoulcn/php/web", 0777);
mkdir ("Leapsoulcn/php/web/test", 0777);

Description: In this code, first use the PHP directory function mkdir to create the home directory LEAPSOULCN, and created two subdirectories, Leapsoul and PHP, in the PHP directory created the web and the test directory.
Knowledge Point: mkdir is primarily used to create catalogs, there are two parameters: New directory name (note that when creating a multilevel directory, you must include the directory path), the new directory access, that is, the umask value, the first number is usually 0, the second number specifies the owner concession, and the third number specifies the license of the owner user group, The fourth number has a global concession and the available values are as follows:
1 = Executable
2 = writable
4 = readable
Add up to three numbers, 7 represent all permissions, and you can give different permissions to the new directories you create according to your needs.
Step Two: Create the Leapsoulcn.txt file in the leapsoulcn/php/directory and write the relevant content
Copy Code code as follows:

@ $fp = fopen ("Leapsoulcn/php/leapsoulcn.txt", "w");
if (! $fp) {
echo "System error";
Exit ();
}else {
$fileData = "domain". " \ t "." "." \ n ";
$fileData = $fileData. " Description "." \ t "." PHP Web site Development Tutorial Network, PHP Beginners for the PHP tutorial network. "." \ n ";
$fileData = $fileData. " Title "." \ t "." This example mainly describes the specific application of PHP directory functions: Cover reading directory, create directory, delete directory and other functions;
Fwrite ($fp, $fileData);
Fclose ($FP);

Description: This example code can be used to refer to the introduction of the PHP file to write tutorials.
Step three: Read (traverse) directory name and text file name
Copy Code code as follows:

$dir = Opendir ("LEAPSOULCN");
while ($fileDir = Readdir ($dir)) {
if (!strcmp ($fileDir, ".") | |! strcmp ($fileDir, "..")) {
echo $fileDir. " Directory listing:<br/><br/> ";
$subDir = "leapsoulcn/". $fileDir;
$dirC = "->";
Listsubdir ($subDir, $dirC);
Closedir ($dir);

Description: In this code example tutorial the main use of PHP directory Functions opendir (), Readdir (), Closedir ().
Knowledge Points:
1, the Opendir function is used to open the tour of the specific directory, the function parameter is the directory name, note that because in this example tutorial PHP execution files and tour of the home directory at the same level, so the parameters passed is only directory name, if not at the same level or read multilevel directory, You need to take a specific directory path or file path.
2, after reading the home directory through the Opendir function, through the while loop to further read the multi-level directories and files in the home directory, the PHP directory function used here is Readdir, this function reads the directory or file name from the directory, when there is no readable directory or file, return false, Note that the Read directory contains the. And ..., in this example tutorial because it is a level down reading directory, so when the read directory information is. To continue reading the next level of the table of contents when jumping out of this cycle.
3, after reading all subdirectories and files of the home directory, through the PHP directory function closedir to close the directory handle, similar to the Fclose function to close the file.
Step Fourth: Create recursive functions for read (traversal) directories and files
Copy Code code as follows:

function Listsubdir ($dirInfo, $dirC)
if (Is_dir ($dirInfo)) {
$subDir = Dir ($dirInfo);
while ($subFile = $subDir->read ()) {
if (!strcmp ($subFile, ".") | |! strcmp ($subFile, "..")) {
$newDir = $dirInfo. " /". $subFile;
if (Is_file ($newDir)) {
Echo $dirC. $subFile. " : File attributes <br/> ";
Echo $dirC. $subFile. " : Directory Properties <br/> ";
Listsubdir ($newDir, "-". $dirC);
$subDir->close ();
else return;

Note: This function has two parameters: the directory (including directory path) that needs to be read, and displays the multilevel directory separator. In this function, the main use of the PHP file directory function Is_dir,is_file,dir class.
Knowledge Points:
1, first through the is_dir to determine whether to read the directory or file, the function of the parameters and Opendir function Similar, note the directory path problem.
2, if the judge needs to read the directory, then through the Dir directory class to further read its multi-level subdirectories, layer by level recursion. The function functions of the Dir class are the same as those of Opendir, Readdir, Closedir and PHP directory functions.
Now that the entire directory is created, the code instance that reads the directory is complete, listing the multilevel subdirectory names and the text file names under the home directory.
How do I delete a directory?
Delete directory you can use the PHP directory function rmdir, function parameters and mkdir function parameters are similar, you can use the relative directory path or absolute directory path, only the directory to be deleted must be empty directory, through the above code instance you can determine what is empty directory.
Through the application of these basic PHP directory functions and file operation functions, can fully implement and file system, write a directory with the creation, delete directories, read directories, management files of the Site Directory file management system, that file information, file size how to read? How do I delete or move a file? Oh, let's share it next time.

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: 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.