Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use unlink to delete a folder in php

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you about how to use unlink to delete folders in php. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

The method to delete a folder using unlink in php: [if ($handle = opendir ("$dirName")) {while (false! = ($item = readdir ($handle) {if ($item...].

Deleting files in PHP is actually a very simple thing, because PHP has prepared the appropriate functions for us, the unlink and rmdir functions. The recursive delete operation can also be completed by using these two functions.

Next, let's take a look at the specific implementation code!

The implementation code is as follows:

All files in the class shanchu {/ / circular directory function delFileUnderDir ($dirName= ".. / Smarty/templates/templates_c") {if ($handle = opendir ("$dirName")) {while (false! = ($item = readdir ($handle) {if ($item! = "." & $item! = "..") {if (is_dir ("$dirName/$item")) {delFileUnderDir ("$dirName/$item") } else {/ / Open source code phpfensi.com if (unlink ("$dirName/$item")) echo "successfully deleted file: $dirName/$itemn";} closedir ($handle);}

Suppose you need to delete all the files in a directory called "upload", but you don't need to delete the directory folder, you can do it with the following code:

Php deletes all directories as follows:

Function deltree ($pathdir) {echo $pathdir;// if (is_empty_dir ($pathdir)) / / if it is empty {rmdir ($pathdir); / / directly delete} else {/ / otherwise read this directory, except. And... Outside $d=dir ($pathdir); while ($if ($pathdir.'/'.$a)-> read ()) {unlink ($pathdir.'/'.$a) } / / if it is a file, delete if directly (is_dir ($pathdir.'/'.$a) & & ($astute roomroom.`) & ($astute roomroom.directory) {/ / if it is a directory if (! is_empty_dir ($pathdir.'/'.$a)) / / whether it is empty {/ / if not, call itself, but the original path + his subordinate directory name deltree ($pathdir.'/'.$a) } if (is_empty_dir ($pathdir.'/'.$a)) {/ / delete rmdir ($pathdir.'/'.$a) directly if it is empty;} $d-> close (); echo "must delete all files in the directory first"; / /}} function is_empty_dir ($pathdir) {/ / determine whether the directory is empty $d=opendir ($pathdir); $iTun0; while ($a=readdir ($d)) {$icalendar;} closedir ($d) If ($I > 2) {return false;} else return true;}

PHP deletes the directory and all files under the directory, as follows:

The above is how to use unlink to delete folders in the php shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report