Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use rmdir () to delete a non-empty directory in php

Shulou Source: shulou.com Published: 2022-06-03 07:21:03 10月05日 Update

This article will explain in detail how to use rmdir () to delete non-empty directories in php. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Delete method: 1, use the scandir () function and foreach statement to traverse all files and folders under the specified directory; 2, use the recursive way to delete all files and folders under the specified directory one by one, and turn the directory into an empty directory; 3, use the "rmdir (directory path)" statement to delete the specified directory.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

You can use the rmdir () function in PHP to delete a specified directory, and the syntax format of this function is as follows:

Rmdir (string $dirname [, resource $context])

Where the parameter $dirname is the directory path to be deleted, and $context is an optional parameter that specifies the environment of the file handle.

Note: when you delete a specified directory using the rmdir () function, the directory must be empty and have the appropriate permissions. The function returns TRUE when it succeeds, and FALSE if it fails. Deleting a directory that is not empty will also generate an E_WERNING-level error.

So what if you need to delete a non-empty directory using rmdir ()?

We can iterate through all the files and folders in this directory and delete them one by one recursively. The following is demonstrated by the sample code:

Description:

The scandir () function returns an array containing all files and directories in the specified directory

This is the end of the article on "how to use rmdir () to delete non-empty directories in php". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Tags: Directories files functions folders empty directories articles paths recursion parameters subitems subdirectories methods more environment statements good practical successful code content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Microsoft vpn MariaDB Shulou Tech Info