Get the App
SLTechnology News&Howtos  ›  Development  › 

How php deletes the current folder

Shulou Source: shulou.com Published: 2022-06-03 19:52:02 11月08日 Update

This article focuses on "how to delete the current folder by php". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to delete the current folder by php.

Operating environment of this article: Windows7 system, PHP7.1 version, Dell G3 computer

How does php delete the current folder?

PHP deletes all files in the current directory and its directory

Use PHP to traverse all directories and files under a directory, and delete all subdirectories and files under the directory. This code is realized by recursion.

Functions used:

Scandir ($path) iterates through all the files in a directory and returns an array.

Unlink ($filename) deletes the file.

Rmdir ($path) deletes only empty folders.

PHP Code:

/ * delete all directories and files under the current directory and its directory * @ param string $path directories to be deleted * @ note $path path without slashes / (for example: correct [$path='./linuxidc/image'] Error [$path='./linuxidc/image/']) * / function deleteDir ($path) {if (is_dir ($path)) {/ / scan all directories and files in a directory and return the array $dirs = scandir ($path) Foreach ($dirs as $dir) {/ / exclude the current directory (.) And the next level directory (..) If ($dir! ='.'& & $dir! ='..') {/ / Recursive subdirectories if it is a directory, continue with $sonDir = $path.'/'.$dir; if (is_dir ($sonDir)) {/ / Recursively delete deleteDir ($sonDir) / / delete the empty directory @ rmdir ($sonDir) after deleting the subdirectories and files in the directory;} else {/ / if the files are deleted directly @ unlink ($sonDir);} @ rmdir ($path);}}

At this point, I believe you have a deeper understanding of "php how to delete the current folder". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Directory file folder subdirectory recursion code content array learning practical deeper interest function practical simple operation slash mode method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology OPPO Reno Shulou Tech Info Huawei Linux