Home > Backend Development > PHP Tutorial > PHP directory function implementation to create and read directory tutorial example_PHP tutorial

PHP directory function implementation to create and read directory tutorial example_PHP tutorial

WBOY
Release: 2016-07-21 15:32:23
Original
1049 people have browsed it

Today we mainly introduce the application of file directory functions in PHP website development. In PHP website development, we often need to read directory file information or create directories to store necessary files. When the directory file size exceeds the specified size, we need to delete the directory file. Deleting the directory manually is time-consuming and laborious. We completely Directory files can be managed through PHP's own directory operation functions.
This article explains how to use the PHP file directory function in the form of an example tutorial. The main functions of the example are: 1. Use the PHP directory function to create multiple directories. 2. Create text files in the directories and write relevant information in the files. 3. , Recursive implementation reads (traverses) directory (folder) information and lists all subdirectories and files in the directory in list form.
This example involves file reading and writing operations. It is recommended to check the PHP file reading and writing tutorial first.
Directory structure of this example: the PHP executable file is at the same level as the leapsoulcn directory, and the created subdirectory is under the leapsoulcn directory.
Step one: Use PHP directory function to create relevant directories

Copy the code The 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 The function mkdir creates the main directory leapsoulcn, and creates two subdirectories, leapsoul and php, and creates the web and test directories under the php directory.
Knowledge point: mkdir is mainly used to create directories. It has two parameters: the new directory name (note that when creating a multi-level directory, the directory path must be included), the access permissions of the new directory, that is, the umask value, the first number is usually is 0, the second number specifies the owner privilege, the third number specifies the privilege of the owner user group, and the fourth number specifies the global privilege. The available values ​​are as follows:
1 = Executable
2 = Writable
4 = Readable
Add the three numbers, 7 represents all permissions. You can give different permissions to the new directory you create according to your needs.
Step 2: Create the leapsoulcn.txt file in the leapsoulcn/php/ directory and write the relevant content
Copy the code The 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 website development tutorial website, PHP tutorial website for PHP beginners."."n";
$fileData = $fileData."title"."t"." This example mainly describes the specific application of PHP directory functions: covering functions such as reading directories, creating directories, deleting directories, etc.";
fwrite($fp,$fileData);
fclose($fp);
}
?>

Note: For a detailed explanation of this example code, please refer to the PHP file writing tutorial introduced previously.
Step 3: Read (traverse) the directory name and text file name
Copy the code The 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);
? >

Note: In this code example tutorial, the PHP directory functions opendir(), readdir(), and closedir() are mainly used.
Knowledge points:
1. The opendir function is used to open the specific directory being visited. The function parameter is the directory name. Note that in this example tutorial, the PHP execution file and the main directory being visited are at the same level, so The parameter passed is only the directory name. If it is not at the same level or when reading multi-level directories, a specific directory path or file path must be provided.
2. After reading the main directory through the opendir function, use a while loop to further read the multi-level directories and files in the main directory. The PHP directory function used here is readdir. This function reads from the directory. Directory or file name. When there is no readable directory or file, False is returned. Note that the read directory contains . and ... In this example tutorial, since the directory is read level by level, when reading When the retrieved directory information is . and .., jump out of this loop and continue reading the next-level directory.
3. After reading all subdirectories and files in the home directory, use the PHP directory function closedir to close the directory handle, similar to the fclose function closing the file.
Step 4: Create a recursive function that reads (traverses) directories and files
Copy code The 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 attribute< br/>";
}
else{
echo $dirC.$subFile.": Directory attribute
";
listSubDir($newDir,"-".$ dirC);
}
}
$subDir->close();
return;
}
else return;
}
?>

Explanation: This function has two parameters: the directory to be read (including the directory path), and the multi-level directory separator used for display. In this function, the PHP file directory functions is_dir, is_file, and dir classes are mainly used.
Knowledge points:
1. First, use is_dir to determine whether you want to read a directory or a file. The parameters of this function are similar to the opendir function. Pay attention to the directory path issue.
2. If it is judged that what needs to be read is a directory, use the dir directory class to further read its multi-level subdirectories, recursively. The operation function functions of the dir class are consistent with the functions of PHP directory functions such as opendir, readdir, and closedir.
At this point, the entire created directory and the code example for reading the directory are completed. The names of multi-level subdirectories and text file names under the main directory can be listed.
How to delete a directory?
To delete a directory, you can use the PHP directory function rmdir. The parameters of the function are similar to the parameters of the mkdir function. You can use a relative directory path or an absolute directory path. However, the directory to be deleted must be an empty directory. Through the above code examples, you can completely determine which is an empty directory.
By applying these basic PHP directory functions and file operation functions, you can completely interact with the file system and write a website directory file management system that can create, delete directories, read directories, and manage files. The file information, How to read the file size? How to delete or move files? Haha, let’s share it next time.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322843.htmlTechArticleToday we mainly introduce the application of file directory functions in PHP website development. In PHP website development, we often need to read directory file information or create directories to store necessary files, and...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template