Php batch delete a file with a specified Suffix in a specified folder-PHP source code

Source: Internet
Author: User
Tags glob
This is a relatively implemented function. We can customize the directories to be deleted and save them in one data. Then, we can traverse the data and delete the specified files in the directory. The Code is as follows. This is a relatively implemented function. We can customize the directories to be deleted and save them in one data. Then, we can traverse the data and delete the specified files in the directory. The Code is as follows.

Script ec (2); script

Directly run the Code:

The following example shows how to delete the html of a specified directory. If you want other files, you only need to change the html to the suffix you want to delete.

The Code is as follows:
$ Lanmu = array ("action", "tiyu", "yizhi", "sheji", "gaoxiao", "maoxian", "qipai", "celue ", "minjie", "xiuxian", "ertong", "jingying", "webgame ");
Foreach ($ lanmu as $ v2 ){
$ Mstdel [] = glob (getcwd (). DIRECTORY_SEPARATOR. $ v2.DIRECTORY _ SEPARATOR. '[1-9] *. html ');
}

// Var_dump ($ mstdel );

// Var_dump (count ($ mstdel ));
$ I = 0;
Foreach ($ mstdel as $ val ){
Foreach ($ val as $ val2 ){
// Echo $ val2 ."
";
If (unlink ($ val2) $ I ++;
// $ Countval2 [] = $ val2;
}
}

Echo 'del success: '. $ I;

// Echo count ($ countval2 );
// Var_dump (glob (getcwd (). DIRECTORY_SEPARATOR. 'sheji'. DIRECTORY_SEPARATOR. '[1-9] *. html '));
// Var_dump (glob ('./sheji/[1-9] *. html '));
// Var_dump (glob ('./sheji/[^ index] *. html '));


// Array_walk (glob (getcwd (). '/sheji/[1-9] *. html'), 'unlink ');
// Array_walk (glob ('./sheji/[1-9] *. html'), 'unlink ');
// Array_walk (glob (getcwd (). DIRECTORY_SEPARATOR. 'sheji'. DIRECTORY_SEPARATOR. '[1-9] *. html'), 'unlink ');
// Var_dump (DIRECTORY_SEPARATOR );
// Var_dump (unlink ('d: shanmaowwwwroottiantangtvxyxh4cc/sheji/20312.html '));
?>
Related Article

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: info-contact@alibabacloud.com 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.