PHP File Deletion unlink () Details

Source: Internet
Author: User

To delete a file in php, you only need to use the unlink function to delete the file. If you want to delete all the files in the directory, You need to delete them using recursive operations.

Please refer to the tutorial on creating a PHP file. We created a file named testfile.txt.

The Code is as follows: Copy code

$ MyFile = "testFile.txt ";
$ Fh = fopen ($ myFile, 'w') or die ("can't open file ");
Fclose ($ fh );
Determine whether the object has been deleted.

$ MyFile = "testFile.txt ";
Unlink ($ myFile );

Example

The Code is as follows: Copy code

$ Filename = 'file.txt ';
Fopen ($ filename, 'a + ');
If (! Unlink ($ filename ))
{
Echo "failed to delete the file {$ filename ";
}
Else
{
Echo "the file {$ filename} is successfully deleted ";
}
?>

Delete all files in the directory

The Code is as follows: Copy code

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 <br/> n ";
}
}
}
Closedir ($ handle );
}
}

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.