Php file deletion code _ PHP Tutorial

Source: Internet
Author: User
Php deletes the file program code. To delete files in php, we need to use the unlink () file deletion function provided by php. next I will introduce you in detail how to delete files using unlink, if you have any need, refer to this tutorial to delete files in php. we need to use the unlink () file deletion function provided by php, next, I will introduce you in detail how to use unlink to delete files. if you need it, refer to this tutorial.

Unlink (filename, context)

Example

The code is as follows:

If (unlink ($ file_delete )){
Echo "The file was deleted successfully.", "n ";
} Else {
Echo "The specified file cocould not be deleted. Please try again.", "n ";
}

Determine whether a file exists

The code is as follows:

$ Myfile = "./test1.txt ";
If (file_exists ($ myfile )){
$ Result = unlink ($ myfile );
Echo $ result;
}
?>

Batch delete files

The code is as follows:

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 {
If (unlink ("$ dirName/$ item") echo "successfully deleted the file: $ dirName/$ item
N ";
}
}
}
Closedir ($ handle );
}
} DelDirAndFile ('www. bKjia. c0m ');

Example

Delete files in the directory and specify the files not deleted

The code is as follows:

Header ("content-Type: text/html; charset = utf-8 ");
// Start configuration
$ Path = "."; // The directory to be deleted in some settings. Add path to the current directory, for example, delete path;
$ Guolv = "del. php, install. php, path"; // you can specify the names of files or folders to be filtered.
// The configuration is complete.
If ($ _ GET ['action'] = "del "){
$ File = array_values_recursive (recurdir ($ path, $ guolv ));
Foreach ($ file as $ k => $ v ){
Remove_directory ($ v );
}
} Else {
Echo "your configuration is as follows:

The directory to be deleted is:
";
If ($ path = ".") echo "current directory"; else echo $ path;
Echo"
The files or folders you want to filter include: ". $ guolv ."

If you confirm filtering, click here to delete the corresponding directory and all files under the Directory. if the configuration is incorrect, go to the file to modify it.
";
}


// Delete directories and files
Function remove_directory ($ dir ){
Foreach (glob ($ dir) as $ fn ){
Echo "removing $ fn
N ";
If (! Is_writable ($ fn) @ chmod ($ fn, 0777 );
If (is_dir ($ fn) {@ rmdir ($ fn) ;}else {@ unlink ($ fn );}
}
}
// Scan the Directory
Function recurdir ($ pathname, $ guolv = 'Del. php ')
{
$ Result = array (); $ temp = array ();
// Check whether the directory is valid and readable
If (! Is_dir ($ pathname) |! Is_readable ($ pathname ))
Return null;
// Obtain all the folders in the directory
$ Allfiles = scandir ($ pathname );
Foreach ($ allfiles as $ key => $ filename)
{
// If it is "." or "...", it is skipped.
If (in_array ($ filename, array ('.', '..') continue;
If (count ($ guolv)> 0) {$ lv = explode (",", $ guolv); if (in_array ($ filename, $ lv) continue ;}

// Obtain the complete file name
$ Fullname = $ pathname. "/". $ filename;
// If the file is a directory, recursively call recurdir
$ Temp [] = $ fullname;
If (is_dir ($ fullname )){
$ Nowpath = explode ("/", $ fullname );
If (count ($ guolv)> 0) {$ lv = explode (",", $ guolv); if (in_array ($ nowpath [count ($ nowpath)-1], $ lv) continue ;}
$ Result [$ filename] = recurdir ($ fullname );}
}
// Add the content in the temporary array to the result array to ensure that the directory is in the front and the file is in the back
Foreach ($ temp as $ f ){
$ Result [] = $ f;
}
Return $ result;
}
// Obtain all objects
Function array_values_recursive ($ ary)
{
$ Lst = array ();
Foreach (array_keys ($ ary) as $ k ){
$ V = $ ary [$ k];
If (is_array ($ v) {$ lst = array_merge ($ lst, array_values_recursive ($ v);} else {$ lst [] = $ v ;}
}
Return $ lst;
}
?>

Upload () file deletion function. next I will introduce you in detail how to use unlink to delete files. if you need it, please refer to this tutorial...

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.