Today mainly introduces the application of the file directory function in the development of PHP Web site. In the development of PHP Web site, we often need to read the directory file information or create a directory to hold the necessary files, and when the size of the directory file exceeds the specified size, we need to delete the directory files, such as manually delete the directory is time-consuming and laborious, we can fully use PHP's own directory operation function to manage the directory file.
This article explains how to use PHP file directory functions as an example tutorial, the main functions of the instance: first, the use of PHP directory functions to create multiple directories, two, in the directory to create a text file and write related information in the file, three, recursive implementation read (Traverse) directory (folder) Information and lists all subdirectories and files in the directory as a list.
This example relates to file read and write operations, it is recommended to first read the PHP file reading and writing tutorial.
This example directory structure: PHP execution files and LEAPSOULCN directory at the same level, the creation of subdirectories in the LEAPSOULCN directory.
First step: Create the relevant directory using PHP directory functions
Copy CodeThe code is as follows:
mkdir ("LEAPSOULCN", 0777);
mkdir ("Leapsoulcn/leapsoul", 0777);
mkdir ("leapsoulcn/php", 0777);
mkdir ("Leapsoulcn/php/web", 0777);
mkdir ("Leapsoulcn/php/web/test", 0777);
?>
Note: 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 mainly used to create a directory, there are two parameters: the new directory name (note that when creating a multilevel directory, must include the directory path), the new Directory access rights, that is, the umask value, the first number is usually 0, the second number specifies the owner concession, the third number specifies the owner user group concessions, The fourth number has a global concession and the available values are as follows:
1 = Executable
2 = writable
4 = readable
Add three numbers, 7 means you have all the permissions, and you can give different permissions to the new directory you create, depending on your needs.
Step Two: Create the Leapsoulcn.txt file in the leapsoulcn/php/directory and write the relevant content
Copy CodeThe code is as follows:
@ $fp = fopen ("Leapsoulcn/php/leapsoulcn.txt", "w");
if (! $fp) {
echo "System error";
Exit ();
}else {
$fileData = "domain". " \ t "." Www.jb51.net "." \ n ";
$fileData = $fileData. " Description "." \ t "." PHP Web Development Tutorial Network, for PHP beginners PHP Tutorial Network. "." \ n ";
$fileData = $fileData. " Title "." \ t "." This example mainly describes the PHP directory functions of the specific application: to read directories, create directories, delete directories and other functions ";
Fwrite ($fp, $fileData);
Fclose ($FP);
}
?>
Description: This example code specific explanation can refer to the previous introduction of the PHP file writing tutorial.
Step three: Read (traverse) directory names and text file names
Copy CodeThe code is as follows:
$dir = Opendir ("LEAPSOULCN");
while ($fileDir = Readdir ($dir)) {
if (!strcmp ($fileDir, ".") | |! strcmp ($fileDir, "..")) {
Continue
}
echo $fileDir. " Directory list:
";
$subDir = "leapsoulcn/". $fileDir;
$dirC = "--";
Listsubdir ($subDir, $dirC);
}
Closedir ($dir);
?>
Description: The PHP directory Functions opendir (), Readdir (), Closedir () are mainly used in this code example tutorial.
Knowledge Points:
1, the Opendir function is used to open the specific directory visited, the function parameter is the directory name, note, because in this example tutorial PHP execution file and tour of the main directory at the same level, so the parameters passed only the directory name, if not at the same level or read the multilevel directory, You need to bring a specific directory path or file path.
2, after reading the main directory through the Opendir function, through the while loop to further read the main directory of the multi-level directory and files, the PHP directory function used here is Readdir, this function reads the directory or the file name from the directory, when there is no readable directory or file, return false, Note that the Read directory contains. And: In this example tutorial because the directory is read down from the first level, the directory information read is. and. When you jump out of this loop, continue reading the next level of directory.
3, after reading all the subdirectories and files of the main directory, through the PHP directory function closedir to close the directory handle, similar to the Fclose function close the file.
Fourth step: Create recursive functions to read (Traverse) directories and files
Copy CodeThe code is as follows:
function Listsubdir ($dirInfo, $dirC)
{
if (Is_dir ($dirInfo)) {
$subDir = Dir ($dirInfo);
while ($subFile = $subDir->read ()) {
if (!strcmp ($subFile, ".") | |! strcmp ($subFile, "..")) {
Continue
}
$newDir = $dirInfo. " /". $subFile;
if (Is_file ($newDir)) {
Echo $dirC. $subFile. " : File properties
";
}
else{
Echo $dirC. $subFile. " : Directory Properties
";
Listsubdir ($newDir, "-". $dirC);
}
}
$subDir->close ();
Return
}
else return;
}
?>
Description: This function has two parameters: the directory to be read (including the directory path), and the multilevel directory delimiter to display. In this function, the main use of 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, pay attention to the directory path problem.
2, if the judge needs to read the directory, then through the Dir directory class to further read its multilevel subdirectory, layer recursion. The function functions of the Dir class are consistent with the functions of Opendir, Readdir, Closedir, PHP directory functions.
At this point, the entire directory is created, and the code instance that reads the directory is completed, listing the multilevel subdirectory name and the text file name in the home directory.
How do I delete a directory?
Delete directory can use PHP directory function rmdir, function parameters and mkdir function parameters similar, you can use relative directory path or absolute directory path, just to delete the directory must be empty directory, through the above code instance you can completely determine which is empty directory.
Through the application of these basic PHP directory functions and file manipulation functions, can fully implement and file system to deal with, write a file with Create, delete directories, read directories, manage files of the Site Directory files management system, the file information, file size how to read? How do I delete or move a file? Oh, let's share it next time.
http://www.bkjia.com/PHPjc/322843.html www.bkjia.com true http://www.bkjia.com/PHPjc/322843.html techarticle today mainly introduces the application of the file directory function in the development of PHP Web site. In the development of PHP Web site, we often need to read the directory file information or create a directory to hold the necessary files, and ...