Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use unlink to delete a folder in php

Shulou Source: shulou.com Published: 2022-06-03 09:45:26 10月13日 Update

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.

Tags: Directories files code folders functions success content can be passed analysis loop appropriate subordinate professional two small and medium things rich content sibling that is articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Xiaomi Docker NVidia Apple