How to delete a folder in thinkphp

PHPz
Release: 2023-04-10 10:15:11
Original
1130 people have browsed it

In web development, deleting folders is a very common operation, and ThinkPHP, as a widely used PHP framework, also provides very convenient file operation functions, allowing us to easily complete folder operations. This article will introduce you how to delete a folder using ThinkPHP.

1. Delete an empty folder

To delete an empty folder, we can use PHP’s built-inrmdir()function, which can directly delete an empty file folder. In ThinkPHP, we only need to use the path parameter of thermdir()function to delete the specified folder. For example:

$path = './test'; //要删除的文件夹路径 if(is_dir($path)){ rmdir($path); }
Copy after login

In the above example, first we define the path of the folder to be deleted, and then use theis_dir()function to determine whether the path is a directory. If it is a directory, execute itrmdir()function to delete it. It should be noted that this method can only delete empty folders. If there are files or subfolders in the folder, they cannot be deleted.

2. Delete non-empty folders

If you want to delete non-empty folders, we can use thedelDir()function to achieve it. The following is a simple implementation:

function delDir($path){ if(is_dir($path)){ if ($dh = opendir($path)){ while (($file = readdir($dh)) !== false){ if ($file != '.' && $file != '..'){ $fullpath = $path.'/'.$file; if(!is_dir($fullpath)){ unlink($fullpath); }else{ delDir($fullpath); } } } closedir($dh); rmdir($path); } } }
Copy after login

delDir()The function is to delete a directory. It calls itself recursively, first deleting all files in the directory, and then deleting the directory. The specific implementation method is to first use theopendir()function to open the specified directory, and then use thereaddir()function to read all files and folders in the directory to determine whether they are .and .., if not handled in the same way.

If it is a file, use theunlink()function to delete it directly; if it is a folder, call thedelDir()function recursively to delete the folder and its contents. Finally, use thermdir()function to delete the empty directory.

3. Summary

This article introduces how to delete empty folders and non-empty folders in ThinkPHP. These two methods can easily meet our daily development needs. However, in actual use, special attention needs to be paid to the permissions of files and folders to avoid being unable to delete them due to permission issues. At the same time, for large folders, the deletion process may be time-consuming, so you need to pay attention to the problem of long execution time when using it.

The above is the detailed content of How to delete a folder in thinkphp. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!