PHP file Removal unlink () detailed _php tutorial

Source: Internet
Author: User
Deleting files in PHP is simple as long as you use the Unlink function to complete the deletion, if you want to delete all the files in the directory we need to use the recursive operation directory to delete.

Remember the lesson created from PHP files, 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 if it was deleted.

$myFile = "TestFile.txt";
Unlink ($myFile);

Cases

The code is as follows Copy Code

$filename = ' file.txt ';
fopen ($filename, ' A + ');
if (!unlink ($filename))
{
echo "File {$filename} delete failed";
}
Else
{
echo "File {$filename} deleted successfully";
}
?>

Delete all files under 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 file: $dirName/$item
n ";
}
}
}
Closedir ($handle);
}
}

http://www.bkjia.com/PHPjc/633098.html www.bkjia.com true http://www.bkjia.com/PHPjc/633098.html techarticle deleting files in PHP is simple as long as you use the Unlink function to complete the deletion, if you want to delete all the files in the directory we need to use the recursive operation directory to delete. Please remember from PHP files ...

  • 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.