PHP directory function implementation to create read directory tutorial instances

  • 2020-03-31 21:26:27
  • OfStack

Today I will introduce the application of file directory function in PHP website development. In PHP web development, we often need to read the directory file information or create a directory to hold the necessary documents, and when the directory file size within the prescribed size we need to delete the directory file, such as manually delete the directory that is time-consuming and laborious, we can through the PHP's built-in the management of the catalog file directory operations function.
In this paper, in the form of instance tutorial shows you how to use the PHP file directory functions, the main functions of the instance: a directory, using PHP function to create multiple directories, second, in the directory to create a text file and write relevant information in the file, read three, recursive implementation (traverse) directory (folder) information and listed in the form of a list of all directories and files in the directory.
This example involves file reading and writing operations, and it is recommended that you take a look at the PHP file reading and writing tutorial first.
This example directory structure: the PHP execution file is at the same level as the leapsoulcn directory, and the created subdirectory is in the leapsoulcn directory.
Step 1: use the PHP directory function to create the associated directory
 
<? 
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, the PHP directory function mkdir was used to create the main directory, leapsoulcn, and created two subdirectories, leapsoul and PHP, in the PHP directory to create the web and test directory.
Knowledge: mkdir mainly used to create the directory, there are two parameters: the new directory name (note that create multi-level directory must contain the directory path), a new directory access permissions, namely the umask value, usually the first number is 0, the second number specifies the owner charter, the third number specifies the franchise owner user group, the fourth number has established a global franchise, available values are as follows:
1 = executable
2 = to write
4 = readable
Add the three Numbers together, and 7 means that you have all the 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 it
 
<? 
@$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 net, oriented PHP A beginner's PHP Tutorial on web. "."n"; 
$fileData = $fileData."title"."t"." This example mainly describes PHP Directory function specific application: covers the reading directory, create directory, delete directory and other functions "; 
fwrite($fp,$fileData); 
fclose($fp); 
} 
?> 

This example code specific explanation can refer to the previous introduction of PHP file writing tutorial.
Step 3: read (traverse) the directory name and the text file name
 
<? 
$dir = opendir("leapsoulcn"); 
while ($fileDir = readdir($dir)) { 
if (!strcmp($fileDir,".")||!strcmp($fileDir,"..")) { 
continue; 
} 
echo $fileDir." Directory list: <br/><br/>"; 
$subDir = "leapsoulcn/".$fileDir; 
$dirC = "->"; 
listSubDir($subDir,$dirC); 
} 
closedir($dir); 
?> 

Note: in this code example tutorial, the main use of PHP directory functions opendir(), readdir(), closedir().
Knowledge:
1, opendir function used to open the specific directory, function parameters for the directory name, note that because of the examples in this tutorial PHP executable files and visit the home directory at the same level, so the parameters passed just directory name, if not in the same level or multi-level directory is read, need to take a specific directory path, or file path.
2. After reading the main directory through the opendir function, further read the multilevel directory and files under the main directory through the while loop. The PHP directory function used here is readdir. In this example tutorial, since the directory is read down one level, the directory information to be read is. And.. , and continue to read the next level of directory.
3. After reading all the subdirectories and files in the home directory, close the directory handle with the PHP directory function closedir, similar to closing the file with the fclose function.
Step 4: create a recursive function that reads (traverses) directories and files
 
<? 
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 <br/>"; 
} 
else{ 
echo $dirC.$subFile." : directory properties <br/>"; 
listSubDir($newDir,"-".$dirC); 
} 
} 
$subDir->close(); 
return; 
} 
else return; 
} 
?> 

Note: this function takes two arguments: the directory to be read (including the directory path), and displays a multilevel directory separator. In this function mainly USES the PHP file directory function is_dir, is_file, dir class.
Knowledge:
1. First, is_dir is used to determine whether a directory or a file is to be read. The parameters of this function are similar to the opendir function.
2. If it is determined that a directory needs to be read, the dir directory class is used to further read its multi-level subdirectories, layer by layer recursively. The dir class has the same function as opendir, readdir, closedir and other PHP directory functions.
At this point, the entire directory creation, reading the directory's code instance is complete, you can list the home directory under the multilevel subdirectory name and the text file name.
How do I delete a directory?
Delete directory 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, but to remove the directory must be empty, through the code example above you can determine which directory is empty.
Through the application of these basic PHP directory function and file operation function, can completely achieve and file system, write a site directory file management system with the creation, delete directory, read directory, management files, the file information, file size how to read? How to delete or move files? Well, let's share it next time.

Related articles: