PHP deletes all subfolders under the folder at a specified time (after a few seconds)

Source: Internet
Author: User
* Delete all subfolders under the folder at the specified time (after a few seconds)
* @param $dir String file path
* @param $sec String deletion time
* @param $file _type string file type
function Delfile ($dir, $sec = ' _type= ', $file ') {
if (Is_dir ($dir)) {
$files = Scandir ($dir);
Open the directory//list all the files in the directory and remove them. And..
foreach ($files as $filename) {
Get File creation time
$worn _time=filectime ($dir. $filename);
$new _time=time ();
$time = $new _time-$worn _time;
if ($time > $sec) {
if ($filename!= '. ' && $filename!= ' ... ') {
if (!is_dir ($dir. '/'. $filename)) {
if (Empty ($file _type)) {
Unlink ($dir. '/'. $filename);
if (Is_array ($file _type)) {
Matches the specified file in a regular match
if (Preg_match ($file _type[0], $filename)) {
Unlink ($dir. '/'. $filename);
Specify files that contain certain strings
if (False!=stristr ($filename, $file _type)) {
Unlink ($dir. '/'. $filename);
Delfile ($dir. '/'. $filename);
RmDir ($dir. '/'. $filename);
if (file_exists ($dir)) unlink ($dir);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.